小编use*_*851的帖子

SqlAlchemy - 按关系属性过滤

我对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)
Run Code Online (Sandbox Code Playgroud)

我想询问所有患者,其母亲的现象是(例如) == 10

据说,我尝试了很多代码,但我没有得到它.在我看来,逻辑上的解决方案就是

patients = Patient.query.filter(Patient.mother.phenoscore == 10)
Run Code Online (Sandbox Code Playgroud)

因为,您可以.mother.phenoscore在输出时访问每个元素但是,此代码不会这样做.

有没有(直接)过关系属性过滤的可能性(没有编写SQL语句或额外的连接语句),我需要这种过滤器不止一次.

即使没有简单的解决方案,我也很乐意得到所有答案.

python sqlalchemy foreign-keys filter

85
推荐指数
6
解决办法
5万
查看次数

标签 统计

filter ×1

foreign-keys ×1

python ×1

sqlalchemy ×1