为了账号安全,请及时绑定邮箱和手机立即绑定

sqlalchemy table.update.returning 只返回一个值而不

sqlalchemy table.update.returning 只返回一个值而不

慕尼黑的夜晚无繁华 2023-04-25 17:00:39
我正在使用postgresqland sqlalchemy,这里是更新一个表,然后通过使用返回多个值returning:async def update_monitor(id: int, payload: MonitorIn) -> Dict[str, Any]:        utc_now = to_utc(datetime.now())    query = (        MONITOR            .update()            .where(id == MONITOR.c.id)            .values(**payload.dict(), updated=utc_now)            .returning(MONITOR.c.id,                       MONITOR.c.watchlist_id,                       MONITOR.c.term,                       MONITOR.c.match_substring_variations,                       MONITOR.c.nameserver_exclusions,                       MONITOR.c.text_exclusions,                       MONITOR.c.created,                       MONITOR.c.updated)    )    result = await PRIMARY.execute(query=query)    return {'id': result[0],            'watchlist_id': result[1],            'term': result[2],            'match_substring_variations': result[3],            'nameserver_exclusions': result[4],            'text_exclusions': result[5],            'created': result[6],            'updated': result[7]}PRIMARY是一个 postgresql 数据库。import databasesPRIMARY = databases.Database(config.DB_URL)但是,结果是一个int对应于MONITOR.c.id第一个返回值的结果,因此该returning方法似乎只返回一个(第一个)值而不是多个值。我实际上希望返回多个值。据此,它应该支持返回多个值。这里出了什么问题?
查看完整描述

1 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

它看起来像databases图书馆的execute()returnsCursor.lastrowid,如果你想要结果行,请使用fetch_one()//fetch_all()iterate()代替。

请注意,这不是SQLAlchemy 和 的行为returning(),正如这个问题的标题所暗示的那样,而是 的行为databases,它只是使用 SQLAlchemy 核心 DSL 作为定义查询的一种方式。在引擎盖下,它然后将它们编译为文本,并使用它自己的连接池和正在使用的异步驱动程序将语句发送到数据库。


查看完整回答
反对 回复 2023-04-25
  • 1 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信