class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('node.id'))
data = Column(String(50))
children = relationship("Node",
backref=backref('parent', remote_side=[id])
)
使用这种类型的模型时,如何查询父对象的所有链接对象?
换句话说,我需要一个列表,其中包含所有的 child 和 child 的 child ,以及 child 的 child ......
最佳答案
这就是我最终所做的,可能不是最好的,但它奏效了。
pks = []
def get_pk(o):
pks.append(o.pk)
for a in o.attachments:
get_pk(a)
get_pk(parent)
关于python - 我将如何查询使用 sqlalchemy 从邻接列表中检索所有对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273238/