sql alchemy 过滤连接模型属性的结果
我有一个模型 X 和一个模型 Y。Y
包含对 X.id 的外键引用,以及属性 x 可用的相关 X 条目的实例。
x_id = Column(Integer, ForeignKey('xtable.id'))
x = relationship('X')
X 还有一个布尔属性“publish”。
在 Y 上执行查询,如何将结果过滤到 x.publish 为 True 的结果;
我尝试过这样做:
DBSession.query(Y).filter_by(x.publish = True).all()
但这不起作用,我收到一条错误消息,指出关键字不能是表达式。我已经浏览了 sql alchemy 文档来寻找解决方案,但我似乎找不到我要找的东西。有什么建议吗?
I have a model X and a model Y.
Y contains a foreign key reference to X.id, with a instance of the related X entry available by the property x.
x_id = Column(Integer, ForeignKey('xtable.id'))
x = relationship('X')
X also has a boolean property 'publish'.
Executing a query on Y, how can i filter my results to those where x.publish is True;
Ive tried doing something like this:
DBSession.query(Y).filter_by(x.publish = True).all()
But this doesnt work, i get an error saying keyword cant be an expression. Ive looked through the sql alchemy docs for a solution, but i cant seem to find what im looking for. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其他选项是
您可以创建直接的新关系
,这样
会自动加入。
Other option is
You can create direct new relationship
Like
This will do join automatically.
您需要在查询中添加
join
到类X
并使用filter
而不是filter_by
:或在一个go:
有关差异,请参阅此问题使用
过滤器
之间和filter_by
。You need to add a
join
in your query to classX
and usefilter
instead offilter_by
:or in one go:
See this question regarding the difference between using
filter
andfilter_by
.