用下面的表格和映射Sqlalichemy:在parent屬性加載相關對象的ID
class A:
def __init__(self):
self.id = None
self.b_ids = {}
self.b_s = {}
class B:
def __init__ (self):
self.id = None
self.a_id = None
self.a = None
a = Table('t_a', meta,
Column('id',Integer, autoincrement=True, primary_key=True),
)
b = Table('t_b', meta,
Column('id',Integer, autoincrement=True, primary_key=True),
Column('a_id', Integer, ForeignKey('t_a.id')),
)
mapper(A, a)
mapper(B, b, properties={'a' : relationship(A, backref="b_s")})
當我打開「A」我可以在「B_S」屬性相關的「B」的對象。但是我想要的是A.b_ids屬性中相關Bs的ID列表。有沒有辦法做到這一點?
我嘗試:
mapper(A, a, properties={'b_ids' :
column_property(select(
[b.c.id],
a.c.id==b.c.a_id))
})
但它給錯誤: 'ProgrammingError:(ProgrammingError)通過用作表達一個子查詢返回多於一行'
非常感謝您的回答。我已經添加了一些疑問作爲答案,因爲它不符合評論。 – Litty 2012-02-15 04:28:40