我试图在SQLAlchemy中的单个查询中联接三个表,但无法这样做。 到目前为止,我已经做到了:-
Test.query.join(Test.sections).join(QuestionSection.section).all()
我知道这是错误的,但是我试图在一个查询中完成。
这是我的桌子:-
问题部分型号:-
class QuestionSection(Dictifiable, db.Model):
__tablename__ = 'question_section'
section_id = Column(Integer, ForeignKey('section.id'), primary_key=True)
question_id = Column(Integer, ForeignKey('question.id'), primary_key=True)
question = db.relationship('Question', backref='question_section')
section = db.relationship('Section', backref='question_section')
截面表:-
class Section(Dictifiable, db.Model):
__tablename__ = 'section'
id = Column(Integer, Sequence('section_id_seq'), primary_key=True)
test_id = Column(Integer, ForeignKey('test.id'))
name = Column(String(50))
test = relationship("Test", back_populates="sections")
questions = db.relationship('Question',
secondary='question_section')
和测试表:-
id = Column(Integer, Sequence('test_id_seq'), primary_key=True)
name = Column(String(50))
sections = relationship("Section", back_populates="test")
tests = relationship("Corporate", secondary='corporate_test',
back_populates='tests')
答案 0 :(得分:0)
我了解自己做错了什么,我应该像这样加入:-
Test.query.join(Test.sections).join(QuestionSection, QuestionSection.section_id == Section.id).all()