Ho appena incontrato lo stesso problema, e dopo i test ho riscontrato che nessuna di queste risposte è sufficiente.
Attualmente, o come di SQLAlchemy .6+, c'è una soluzione molto semplice (non so se questo esiste nella versione precedente, anche se immagino che fa):
session.refresh()
Quindi, il codice sarebbe simile a questa:
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.
Ecco come farlo.
È possibile inizializzare il motore SA con 'echo = True' e vedere quale SQL viene eseguito in tempo di svuotamento? Quello che descrivi * dovrebbe * funzionare e darti l'id, ma potrebbe esserci qualche altro problema che si traduce in f.id in Nessuno. –