24

Does anybody know what the main difference between session.commit() and session.flush() in SQLAlchemy is?

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
ashiaka
  • 3,994
  • 8
  • 32
  • 45
  • 3
    Which aspect of the documentation ("flushing" and "committing" in http://www.sqlalchemy.org/docs/orm/session.html#flushing) do you find unclear? – NPE Sep 19 '11 at 13:14
  • 1
    Possible duplicate of [SQLAlchemy: What's the difference between flush() and commit()?](https://stackoverflow.com/questions/4201455/sqlalchemy-whats-the-difference-between-flush-and-commit) – flyingdutchman Jul 17 '19 at 07:43

5 Answers5

19

The easiest way I know how to explain what these do is to just show you, using echo=True:

>>> session.flush()
BEGIN (implicit)
INSERT INTO users (username, password) VALUES (?, ?)
('alice', None)
>>> session.commit()
COMMIT
>>> 

flush() causes the data to be sent to the database. commit() causes a COMMIT, which tells the database to keep the data that was just sent. As others have stated, commit() will also cause a flush() to occur, if it's needed.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
16

Here are some relevant quotes from the documentation.

flush:

When the Session is used with its default configuration, the flush step is nearly always done transparently. Specifically, the flush occurs before any individual Query is issued, as well as within the commit() call before the transaction is committed.

commit:

commit() is used to commit the current transaction. It always issues flush() beforehand to flush any remaining state to the database; this is independent of the “autoflush” setting. If no transaction is present, it raises an error. Note that the default behavior of the Session is that a “transaction” is always present; this behavior can be disabled by setting autocommit=True. In autocommit mode, a transaction can be initiated by calling the begin() method.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
7

Straight from the documentation:

commit() is used to commit the current transaction. It always issues flush() beforehand to flush any remaining state to the database.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
3

Although the above answers are correct, the most useful feature of flush is in applying the changes made to the table in the database back into the concerned object in the code. Here is an example, Suppose you register a user and you want to give back his id,

u = User(name,address,phone) #id is autogenerated 
session.add(u)
session.commit() #calls flush beforehand, but we need it after the commit
session.flush() #updates the objects of the session 
print(u.id) #id field of the User object updated after the flush

You need not query again to get his id!! Hope this helps

Chandan Purohit
  • 2,283
  • 1
  • 17
  • 16
0

flush() will synchronize your database with the current state of object/objects held in the memory but it does not commit the transaction. So, if you get any exception after flush() is called, then the transaction will be rolled back. You can synchronize your database with small chunks of data using flush() instead of committing a large data at once using commit() and face the risk of getting an Out Of Memory exception.

commit() will make data stored in the database permanent. There is no way you can rollback your transaction once the commit() succeeds.

Source: https://stackoverflow.com/a/26976077/4115031

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95