SQLAlchemy 获取 AutoField 值

March 17, 2015

f = Foo(bar=x)
session.add(f)
session.flush()
# At this point, the object f has been pushed to the DB, 
# and has been automatically assigned a unique primary key id

f.id
# is None

session.refresh(f)
# refresh updates given object in the session with its state in the DB
# (and can also only refresh certain attributes - search for documentation)

f.id
# is the automatically assigned primary key ID given in the database.

From:

python - sqlalchemy flush() and get inserted id? - Stack Overflow


Profile logo

Written by Alvie Zhang

If you're going through hell, keep going