You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PonyORM select()方法触发RETURN_GENERATOR错误,求问题排查

PonyORM 查询报错:Unsupported operation: RETURN_GENERATOR

你的问题出在缺少PonyORM必须的数据库会话(db_session)上下文——所有数据库操作(包括查询)都得在这个环境里执行,不然ORM没法正确编译和执行查询,就会抛出你看到的DecompileError。

修改后的代码如下:

from pony.orm import Database, PrimaryKey, select, Required, db_session  # 新增导入db_session

db = Database()
db.bind(
    provider='postgres', 
    user='postgres', 
    password='root', 
    host='localhost', 
    database='test')

class TestTable(db.Entity):
    _table_ = "testTable"
    columnOne = PrimaryKey(int, column="columnOne")
    columnTwo = Required(str, column="columnTwo")

db.generate_mapping(create_tables=True)

# 将查询逻辑包裹在db_session上下文管理器中
with db_session():
    result = select(row for row in TestTable)
    # 可以通过list()转换结果为列表查看数据
    print(list(result))

你也可以用装饰器的方式来管理会话,比如把查询逻辑放在被@db_session装饰的函数里:

@db_session
def fetch_test_data():
    result = select(row for row in TestTable)
    return list(result)

# 调用函数执行查询
print(fetch_test_data())

内容的提问来源于stack exchange,提问作者LLL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:12:43