【问题标题】:Confused with multiple Join in single query, Sqlalchemy对单个查询中的多个联接感到困惑,Sqlalchemy
【发布时间】:2019-08-07 20:43:21
【问题描述】:

我正在尝试在 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')

【问题讨论】:

    标签: mysql database sqlalchemy


    【解决方案1】:

    我明白我做错了什么,我应该这样加入:-

    Test.query.join(Test.sections).join(QuestionSection, QuestionSection.section_id == Section.id).all()
    
    

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多