Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force SQLAlchemy to update rows

I'm currently using SQLAlchemy with two distinct session objects. In one object, I am inserting rows into a mysql database. In the other session I am querying that database for the max row id. However, the second session is not querying the latest from the database. If I query the database manually, I see the correct, higher max row id.

How can I force the second session to query the live database?

like image 799
Foo Barrigno Avatar asked Dec 28 '25 01:12

Foo Barrigno


1 Answers

The first session needs to commit to flush changes to the database.

first_session.commit()

Session holds all the objects in memory and flushes them together to the database (lazy loading, for efficiency). Thus the changes made by first_session are not visible to the second_session which is reading data from the database.

like image 59
exAres Avatar answered Dec 30 '25 15:12

exAres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!