I need ID of an object which I've just added using session.add()
. I need the auto-increment ID of this object before committing
session.
If I called the object instance.id, I get None
.
Is there a way to get ID of a object without committing?
autoflush – The autoflush setting to use with newly created Session objects. autocommit – The autocommit setting to use with newly created Session objects. expire_on_commit=True – the Session. expire_on_commit setting to use with newly created Session objects.
Advertisements. In order to interact with the database, we need to obtain its handle. A session object is the handle to database. Session class is defined using sessionmaker() – a configurable session factory method which is bound to the engine object created earlier.
Session. rollback() rolls back the current transaction.
The __repr__ function is defined by the designer of a type, in order to provide a means for users of the type to represent values of that type unambiguously, with a string.
Simple answer: call session.flush()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With