例如(eagerload/joinedload 做同样的事情):
session = Session()
parents = session.query(Parent).options(joinedload(Parent.children)).all()
session.close()
print parents[0].children # This works
print parents[0].children[0].parent # This gives a lazy loading error
在关闭会话之前添加以下循环有效(并且不会命中数据库):
for p in parents:
for c in p.children:
c.parent
这很愚蠢。有没有办法改变原始查询,以便它加载关系的双方而不在输出 SQL 中添加更多连接?
更新如果它是相关的;这是映射
class Parent(Entity):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
children = relation("Child", backref="parent")
class Child(Entity):
__tablename__ = "child"
id = Column(Integer, primary_key=True)
parentId = Column(Integer, ForeignKey("parent.id"), index=True)