给出此模型:
class Record(Base):
__tablename__ = 'records'
id = Column(String, primary_key=True)
# (... some columns ...)
rel1 = relationship('Class1', uselist=False, cascade='all, delete-orphan')
rel2 = relationship('Class2', cascade='all, delete-orphan')
rel3 = relationship('Class3', cascade='all, delete-orphan')
# (... many more relations ...)
如何查询所有关系?例如如果我有一个名为Record
的{{1}}实例,如何避免这样做:
entry
,而是执行以下操作:
entry = session.query(Record).filter(some_criteria).first()
for rel1 in entry.rel1:
print(rel1)
for rel2 in entry.rel2:
print(rel2)
for rel3 in entry.rel3:
print(rel3)
# and so on
我可以通过entry = session.query(Record).filter(some_criteria).first()
for relation in entry.relations:
for entry in relation:
print entry
获取关系,但是如果这样,我不确定如何使用结果。
主要目标是删除所有条目,这是通过entry.__mapper__.relationships
实现的,但是,在删除之前,我想列出所有条目以用于审核目的,并可能需要用户的确认。预先感谢。