Acabo de encontrarme con el mismo problema y, después de probar, descubrí que NINGUNA de estas respuestas es suficiente.
Actualmente, o a partir de sqlalchemy .6+, hay una solución muy simple (no sé si existe en la versión anterior, aunque imagino que sí):
session.refresh ()
Entonces, su código se vería así:
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.
Así es como se hace.
echo=True
y ver qué SQL se ejecuta en el tiempo de descarga? Lo que describa debería funcionar y darle la identificación, pero puede haber algún otro problema que haga que f.id sea None.