That makes sense, thank you, Simon! Regarding the events: you suggest to use a before_flush() to examine session.dirty whenever a session.query() executes?
Also, is there a way to get the list of objects that have been flushed, or should I track them myself whenever a before_flush() event occurs? Jens On Thursday, November 16, 2017 at 7:49:54 PM UTC+10, Simon King wrote: > > On Thu, Nov 16, 2017 at 7:45 AM, <jens.t...@gmail.com <javascript:>> > wrote: > > 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 > > The results you are seeing are due to autoflush, which is on by > default. When you run your second query, the session flushes any > pending changes to the database before running the query. After the > flush, the session is considered clean. The methods you were using > (dirty, is_modified etc.) indicate whether the session contains > changes that haven't been flushed to the database. They don't tell you > if the database has received changes that haven't yet been committed. > > There are various ways to do what you want, depending on how your code > is structured. One possibility is to use a Session event such as > before_flush to set a flag saying that there are uncomitted changes in > the database. > > http://docs.sqlalchemy.org/en/latest/orm/session_events.html > > Hope that helps, > > Simon > -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.