Foo Barrigno
Foo Barrigno

Reputation: 237

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?

Upvotes: 3

Views: 2197

Answers (2)

AMNevess
AMNevess

Reputation: 81

Had a similar problem, for some reason i had to commit both sessions. Even the one that is only reading.

This might be a problem with my code though, cannot use same session as it the code will run on different machines. Also documentation of SQLalchemy says that each session should be used by one thread only, although 1 reading and 1 writing should not be a problem.

Upvotes: 0

exAres
exAres

Reputation: 4926

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.

Upvotes: 4

Related Questions