我正在使用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 作為定義查詢的一種方式。在引擎蓋下,它然后將它們編譯為文本,并使用它自己的連接池和正在使用的異步驅動程序將語句發送到數據庫。
添加回答
舉報
0/150
提交
取消
