SQLAlchemy:在外部属性上使用 like() 进行过滤
我有以下模型:
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))
...
然后我尝试执行以下操作:
state = 'some value'
crsRuns = Session.query(CRRun)
crsRuns = crsRuns.options(eagerload('cr'))
.filter(CRRun.cr != None)
.filter(CRRun.cr.state.like('%' + state + '%'))
但是,这会导致以下错误:
AttributeError:“InstrumentedAttribute”对象和“Comparator”对象都没有属性“state”
如何通过通过外键连接到我正在查询的表的表中的列的值来过滤查询?
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'
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
Try:
我最终做了以下事情:
I ended up doing the following: