[英]Filtering by relation count in SQLAlchemy
I'm using the SQLAlchemy Python ORM in a Pylons project. 我在Pylons项目中使用SQLAlchemy Python ORM。 I have a class "Project" which has a one to many relationship with another class "Entry".
我有一个“Project”类,它与另一个类“Entry”有一对多的关系。 I want to do a query in SQLAlchemy that gives me all of the projects which have one or more entries associated with them.
我想在SQLAlchemy中进行查询,该查询为我提供了与其关联的一个或多个条目的所有项目。 At the moment I'm doing:
目前我正在做:
[project for project in Session.query(Project) if len(project.entries)>0]
which I know isn't ideal, but I can't figure out how to do a filter that does what I require (eg Session.query(Project).filter(Project.entries.exists())). 我知道这不是理想的,但我无法弄清楚如何做一个我需要的过滤器(例如Session.query(Project).filter(Project.entries.exists()))。
Any ideas? 有任何想法吗?
Session.query(Project).filter(Project.entries.any())
should work. Session.query(Project).filter(Project.entries.any())
应该可以工作。
Edited credit of James Brady's comment, be sure to give him some love. 编辑詹姆斯布拉迪的评论,一定要给他一些爱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.