【发布时间】:2010-10-27 11:31:46
【问题描述】:
我正在使用 Turbogears2 和 SQLAlchemy 开发一个 web 应用程序。我有两个映射表 O1 和 O2。 O2 在“ones”中有一个 O1 的排序列表。 在某些时候,我想查询所有 O2 和引用的 O1。
很遗憾,下面的查询失败了,因为表 O2 在查询中具有别名,并且 order_by 短语引用的列不再已知。
我想知道如何解决这个问题,同时尽可能保持声明式语法。
base = declarative_base()
class O1(base):
__tablename__ = 'O1'
value = Column(Integer)
o2_id = Column(Integer, ForeignKey('O1.id')) # The culprit
class O2(base):
__tablename__ = 'O2'
id = Column(Integer, primary_key=True)
ones = relation('O1', order_by = ['O1.value'])
Session.query(O2).options(eagerload('ones')).all() # Throws an error
【问题讨论】:
标签: python orm sqlalchemy