Michael Ekoka
Michael Ekoka

Reputation: 20098

What's the difference between detached objects and expired objects in SQLAlchemy?

I'm not sure that I understand how SQLAlchemy's session tracks objects over the course of its life cycle. I suppose that I'm being confused by possible false positives.

Could someone explain what should happen if I did this with a scoped_session that has expired_on_commit set to the default True :

box = session.query(User).filter(Box.box_id==3, Box.color==u'red').one()
box.color = u'blue'

session.commit() # 1

box.color = u'yellow'

session.commit() # 2

Is session still tracking box after the first commit (#1)? Is box now expired? Is it detached?

If so, why would the second commit still push my changes to the db? Why would it succeed to do so over 400 times and then once, skip a beat (by silently not pushing them).

Upvotes: 0

Views: 393

Answers (1)

davidism
davidism

Reputation: 127330

Detached instances have not been added, or have been expunged, from the session. They are not persisted by commit and may not be able to load deferred, expired, or related fields.

Expired instances are part of the session, but have been marked as needing a refresh of their fields and relationships. Access to the attributes will perform another query to get the latest values. Changes will be persisted on commit.

Upvotes: 1

Related Questions