【发布时间】:2020-12-11 04:45:56
【问题描述】:
-
查询语句:获取名称以“A”开头的子项。将他们与父母联系起来。 架构:
-
我有一对多的关系。 Parent(id, Name, children(rel->child)) -> Child(id, Name)
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
Name = Column(String)
children = relationship("Child", lazy='joined')
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
Name = Column(String)
-
想要过滤关系对象。 (即,当实例化 Parent.children 时获得的对象列表)。
-
例如:
[Parent(id=1, Name='Parent1' children=[Child(id='1', Name='AChild1'), Child(id='2', Name='Child2')]需要在访问结果查询时过滤为:[Parent(id=1, Name='Parent1' children=[Child(id='1', Name='AChild1')]。
如何编写语句来获得上述结果?
A solution that Filters them Once Loaded, I want to Filter them While Loading.
【问题讨论】:
标签: python sql python-3.x sqlalchemy