I'm learning SQLAlchemy ORM and I think I have a handle on the basics. One thing I'm curious about though: When a change is made that implies a database write (for example session.add(my_object)
), that change is treated as pending in the session and isn't written until right before the next query.
What I'm wondering is, are all such writes always written before the next query, or does the next query have to be somehow related to the pending write? For example if I issue the command session.add(my_object)
and then my next command is session.query(ClassOfMyObject).filter(...some condition which would match my_object).all()
, I know that the first write will definitely be flushed before this latter query is executed. But what if the objects are totally unrelated, e.g. session.add(apple)
and session.query(Orange).filter(...)
where there isn't even a foreign key between apples and oranges? Would apple
still be written before we query for an Orange
?
Also, what is the rationale for delaying the flush like this? Why not always flush everything immediately?