filtering by association attributes with SqlAlchemy association_proxy

与世无争的帅哥 提交于 2019-12-03 02:26:59

Use any() method of association proxy:

s.query(Developer).filter(Developer.developerProjects.any(status='active'))
quantka

To add to Denis's answer:

When I tried the any() method, I got this error

sqlalchemy.exc.InvalidRequestError: 'any()' not implemented for scalar attributes. Use has().

Using has() instead of any() worked for me.

It looks like any() is for list relationships (in this case Developers potentially have multiple projects so Developer.developerProjects is a list). Whereas has() is for a single relationship (for instance if a Developer had one Workplace associated).

Here is the link to documentation: http://docs.sqlalchemy.org/en/latest/orm/internals.html#sqlalchemy.orm.properties.RelationshipProperty.Comparator.has

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!