Forcing sqlalchemy ORM get () outside of an identity card

Background

The method get()is special in SQLAlchemy ORM because it tries to return objects from the identity map before issuing the SQL query to the database (see the documentation ).

This is great for performance, but it can cause problems for distributed applications, because the object can be modified by another process, so the local process is not able to know that the object is dirty, and will continue to retrieve the obsolete object from when it is displayed get().


Question

How can I make get()ignore the identification card and call a call in the database every time?


Example

  • I have an object Companydefined in ORM.
  • I have a process price_updater()that updates the attribute of stock_priceall objects Companyevery second.
  • I have a process buy_and_sell_stock()that sometimes buys and sells stocks.
    • Now, inside this process, I could load the object microsoft = Company.query.get(123).
    • In a few minutes I can send another call Company.query.get(123). The stock price has changed since then, but my buy_and_sell_stock()process is not aware of the change because it happened in another process.
    • Thus, the call get(123)returns an outdated version Companyfrom the session identification card, which is a problem.

I did a SO search (under the [sqlalchemy] tag) and read the SQLAlchemy docs to try to figure out how to do this, but couldn't find a way.

+4
1

session.expire(my_instance) . , expire ( expunge), , , . . PostgreSQL ( ), SQLAlchemy .

, in: my_instance in session.

filter get , .

Company.query.filter_by(id=123).one()
+3

All Articles