[Sqlalchemy-tickets] [sqlalchemy] #2743: Backref broken for detached objects
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-06-05 16:14:57
|
#2743: Backref broken for detached objects
----------------------+-----------------------------------------
Reporter: schlamar | Owner: zzzeek
Type: defect | Status: new
Priority: medium | Milestone:
Component: orm | Severity: no triage selected yet
Keywords: | Progress State: awaiting triage
----------------------+-----------------------------------------
You cannot traverse a backref if the object is detached and the
relationship is already joined.
Testcase:
{{{
#!python
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship('Parent', backref='children')
def main():
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
p = Parent()
p.children.append(Child())
session.add(p)
session.commit()
parents = session.query(Parent).options(joinedload('children')).all()
session.expunge_all()
print parents
print parents[0].children
print parents[0].children[0].parent
}}}
Fails with:
{{{
Traceback (most recent call last):
File "testcase.py", line 44, in <module>
main()
File "testcase.py", line 35, in main
print parents[0].children[0].parent
File
"/Users/marc/Documents/python/sqlalchemy/lib/sqlalchemy/orm/attributes.py",
line 316, in __get__
return self.impl.get(instance_state(instance), dict_)
File
"/Users/marc/Documents/python/sqlalchemy/lib/sqlalchemy/orm/attributes.py",
line 613, in get
value = self.callable_(state, passive)
File
"/Users/marc/Documents/python/sqlalchemy/lib/sqlalchemy/orm/strategies.py",
line 497, in _load_for_state
(orm_util.state_str(state), self.key)
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Child at
0x10654ffd0> is not bound to a Session; lazy load operation of attribute
'parent' cannot proceed
}}}
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2743>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|