SqlAlchemy: getting the id of the last record inserted

后端 未结 7 914
[愿得一人]
[愿得一人] 2021-02-05 01:35

I am using SQLAlchemy without the ORM, i.e. using hand crafted SQL statememts to directly interact with the backend db. I am using PG as my backend db (psycopg2 as DB driver) in

7条回答
  •  甜味超标
    2021-02-05 02:26

    Make sure you use fetchrow/fetch to receive the returning object

    insert_stmt = user.insert().values(name="homer", country_id="123").returning(user.c.id)
    
    row_id = await conn.fetchrow(insert_stmt)
    

提交回复
热议问题