[英]SQLAlchemy: filter_by with like() on foreign attribute
I have the following models:我有以下型号:
class CRRun(Base):
...
crID = Column(u'CR_ID', INTEGER(), ForeignKey(CR.id), primary_key=True,
nullable=False)
cr = relationship(CR, backref=backref("CR_RUN", uselist=False))
...
class CR(Base):
...
id = Column(u'CR_ID', INTEGER(), primary_key=True, nullable=False)
state = Column(u'STATE', VARCHAR(20))
...
I am then trying to do the following:然后我尝试执行以下操作:
state = 'some value'
crsRuns = Session.query(CRRun)
crsRuns = crsRuns.options(eagerload('cr'))
.filter(CRRun.cr != None)
.filter(CRRun.cr.state.like('%' + state + '%'))
However, this causes the following error:但是,这会导致以下错误:
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object has an attribute 'state'
AttributeError: 'InstrumentedAttribute' object 和 'Comparator' object 都没有属性 'state'
How can I filter my query by the value of a column in a table that's connected to the table I'm querying via a foreign key?如何通过连接到我通过外键查询的表的表中的列的值过滤我的查询?
Try:尝试:
state = 'some value'
crsRuns = Session.query(CRRun)
crsRuns = crsRuns.options(eagerload('cr')) \
.filter(CRRun.cr.has(
CR.state.like('%' + state + '%')
))
I ended up doing the following:我最终做了以下事情:
crsRuns = Session.query(CRRun, CR, Run).join(CR).join(Run)
crsRuns = crsRuns.filter(CR.state.like('%' + state + '%'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.