Hello,
I've been exploring some of the session functionality that handles object
states, and I'm quite confused. Here is what I see:
>>> engine = engine_from_config({'sqlalchemy.url': 'mysql+pymysql://…'})
>>> session_factory = sessionmaker(bind=engine) # No autoflush
>>> session = session_factory()
>>> # Now query a table to get an object.
>>> p = session.query(Table).filter(Table.id == '0f4…ed6').one_or_none()
>>> p.name
"Some Name"
>>> p.name = "Other Name"
>>> session.dirty
IdentitySet([<Table object at 0x10bb50d68>])
>>> session.is_modified(p)
True
>>> session._is_clean()
False
This all makes sense. If I now create a new query, then the above change
*seems* to be gone, but isn't?
>>> p2 = session.query(Table).filter(Table.id == '384…a05c').one_or_none()
>>> session.dirty
IdentitySet([])
>>> session.is_modified(p)
False # p is not modified according to this.
>>> session._is_clean()
True
>>> p.name
"Other Name" # p still has the modified name.
The new query seems to set the session to "clean", but the object p still
contains its change. I can't quite find documentation for the behavior.
What am I missing?
What I would like to do is: in one session, select a few objects (multiple
queries), inspect, perhaps modify them. Then I'd like to query if there
were any modifications/deletes and if so choose to commit or rollback.
Initially I thought to use the Session.dirty/deleted/new properties, but
then the above showed.
If I was to set autoflush, how could I inspect the flushed code buffer?
Thanks!
Jens
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.