注意:這是在SQLAlchemy 0.5.7中。看來,這是不可能的,因爲「懶惰」不適用於一對一映射。如果確實如此,我認爲它錯了,請發佈您的答案。SQLAlchemy中的UnboundExecutionError
我有以下的方法,該方法應該返回任何類的父Parent
的所有實例。請注意,類Child
有一個屬性fubar
,這是另一個類。我已經在其他地方正確映射了它
def get_all(self):
session = self.__Session()
lst = session.query(Parent).with_polymorphic('*').all()
#print lst # <-- this commented back in and it all works.
session.expunge_all()
session.close()
return lst
然後,我可以做的事情,如for item in get_all(): [...]
和所有。在child
的映射中,我有lazy-False
,所以我會假設它會被正確加載。但是,如果我離開評論在打印行,我得到的是這樣的例外:
UnboundExecutionError: Parent instance <Child at 0xa1bca4c> is not bound to a Session; lazy load operation of attribute 'fubar' cannot proceed
我不明白的事情是這樣的:當打印線是存在的,對象確實是裝的罰款。
我在做什麼錯?
編輯:感謝@van,我認爲fubar
對象確實是以懶惰的方式加載的。我在映射以下幾點:
sqlalchemy.orm.mapper(Child, tb_child, inherits=Parent,
polymorphic_identity='Child', properties={'fubar':
sqlalchemy.orm.relation(Child,
lazy=False,
uselist=False,
cascade="all, delete-orphan")
}
)
父的代碼是這樣的:?
sqlalchemy.orm.mapper(Parent,
tb_parent,
polymorphic_on=tb_parent.c.type,
polymorphic_identity='Parent')
什麼不對這個...
另外,我怎麼能強迫query()
以非懶惰的方式加載所有內容?
「Parent」的'repr'是否也打印出'Child'?所以它是由於非常'印刷'加載?啓用'echo = True'並查看'print'是否在'print'時加載了'Child'。 – van
@van:是的,「家長」的代表確實稱爲孩子的「代表」。是的,它看起來像'fubar'對象是懶加載的。該死!問題已更新。謝謝。 – Sardathrion
但在您的編輯中,我沒有看到任何「fubar」關係定義,只有「ref」...? – van