我对 SQLAlchemy 没有太多经验,但我遇到了一个无法解决的问题。我尝试搜索并尝试了很多代码。
这是我的课程(简化为最重要的代码):
class Patient(Base):
__tablename__ = 'patients'
id = Column(Integer, primary_key=True, nullable=False)
mother_id = Column(Integer, ForeignKey('patients.id'), index=True)
mother = relationship('Patient', primaryjoin='Patient.id==Patient.mother_id', remote_side='Patient.id', uselist=False)
phenoscore = Column(Float)
我想查询所有患者,其母亲的表型评分是(例如)== 10
如前所述,我尝试了很多代码,但我不明白。在我看来,逻辑上的解决方案是
patients = Patient.query.filter(Patient.mother.phenoscore == 10)
因为,您可以访问.mother.phenoscore
对于输出时的每个元素,但是,此代码不会执行此操作。
是否有(直接)可能通过关系的属性进行过滤(无需编写 SQL 语句或额外的连接语句),我多次需要这种过滤器。
即使没有简单的解决方案,我也很高兴得到所有答案。