【发布时间】:2012-03-05 17:56:12
【问题描述】:
具有以下表格和映射
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 属性中相关 B 的 id 列表。有没有办法做到这一点?
我试过了:
mapper(A, a, properties={'b_ids' :
column_property(select(
[b.c.id],
a.c.id==b.c.a_id))
})
但它给出的错误:'ProgrammingError: (ProgrammingError) more than one row returned by a subquery used as an expression'
【问题讨论】:
标签: python orm sqlalchemy