objects where session fell out of scope

Issue #2281 resolved
Mike Bayer repo owner created an issue

need to decide behavior here:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base= declarative_base()

class Character(Base):
    __tablename__ = "characters"

    id = Column(Integer, primary_key=True)

s = Session()

c1 = Character()
s.add(c1)

del s
import gc
gc.collect()

assert object_session(c1) is None

s2 = Session()
s2.add(c1)

raises an error ? or implicit detachment from s?

Comments (3)

  1. Mike Bayer reporter

    and the patch to change this for "implicit detachment" is simply this

    diff -r 36e2b2d8750ca5bcf0345733973f5ed097d2949a lib/sqlalchemy/orm/session.py
    --- a/lib/sqlalchemy/orm/session.py Wed Sep 21 10:26:49 2011 -0400
    +++ b/lib/sqlalchemy/orm/session.py Wed Sep 21 11:55:57 2011 -0400
    @@ -1473,7 +1473,7 @@
                         "present in this session."
                         % (mapperutil.state_str(state), state.key))
    
    -        if state.session_id and state.session_id is not self.hash_key:
    +        if state.session_id and state.session_id is not self.hash_key and state.session_id in _sessions:
                 raise sa_exc.InvalidRequestError(
                     "Object '%s' is already attached to session '%s' "
                     "(this is '%s')" % (mapperutil.state_str(state),
    
  2. Log in to comment