Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy get added object's id without committing session

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?

like image 803
Shafiul Avatar asked Apr 15 '14 06:04

Shafiul


People also ask

What does Autoflush do in SQLAlchemy?

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.

What is Sessionmaker in SQLAlchemy?

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.

What does Session rollback () do?

Session. rollback() rolls back the current transaction.

What is __ repr __ SQLAlchemy?

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.


1 Answers

Simple answer: call session.flush().

like image 163
Matthias Urlichs Avatar answered Sep 18 '22 18:09

Matthias Urlichs