我有两个表项和ItemDescriptions如何根据SqlAlchemy中的表B选择表A中的一个?
class Item(Base):
__tablename__ = "item"
id = Column(Integer, primary_key=True)
description_id = Column(Integer, ForeignKey('item_description.id'))
class ItemDescription(Base):
__tablename__ = "item_description"
id = Column(Integer, primary_key=True)
鉴于ItemDescriptions我想这样的项目有每个ItemDescription ID的一个项目的名单列表。我不在乎哪个项目。
[编辑为清晰起见]
鉴于项目和说明的这个名单:
Item, Description
1 , 1
2 , 1
3 , 1
4 , 2
5 , 2
6 , 3
7 , 3
8 , 3
我想要一个查询,将返回类似:
Item, Description
2 , 1
4 , 2
7 , 3
我无法完成子查询等。
谢谢你的帮助
column_property看起来不错,谢谢!如果我想把它作为单个查询而不是列属性,我会把select放在子查询中吗? – Ben 2011-05-20 21:11:50
是的,你可以将选择移动到查询中。我已经添加了答案。就个人而言,我仍然更喜欢column_property :) – sayap 2011-05-21 01:20:12
我喜欢column_query方法,但也感谢您以查询方式显示。 – Ben 2011-05-24 17:50:48