I'm a bit confused about the difference between (a) an instance "belonging
to" a session as determined by object_session(obj) and (b) "being in" the
session as determined by obj in session. To illustrate:
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
if __name__ == "__main__":
engine = create_engine("sqlite:///:memory:", echo=False)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
ed = User(name="ed")
def log(msg):
print "{}: {}".format(msg, (Session.object_session(ed) is not
None,
ed in session))
log("After instantiation")
session.add(ed)
log("After add")
session.flush()
log("After flush")
session.delete(ed)
log("After delete")
session.flush()
log("After flush")
session.expunge(ed)
log("After expunge")
The output is:
After instantiation: (False, False)
After add: (True, True)
After flush: (True, True)
After delete: (True, True)
After flush: (True, False)
After expunge: (True, False)
So up to the last flush() these two coincide but after flush() deletes the
instance, it still "belongs to" the session while not being in it. The
expunge() at the end has no effect.
- What is the difference between the two?
- How can an instance be dissociated from the session?
- Is a (False, True) combination possible and if yes how?
Thanks,
George
--
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.