filtering by association attributes with SqlAlchemy association_proxy

后端 未结 2 699
日久生厌
日久生厌 2021-02-05 19:23

I have a many-to-many (developers & projects) relationship modeled using SA\'s association_proxy. The collections (developers on each project & project

相关标签:
2条回答
  • 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

    0 讨论(0)
  • 2021-02-05 20:10

    Use any() method of association proxy:

    s.query(Developer).filter(Developer.developerProjects.any(status='active'))
    
    0 讨论(0)
提交回复
热议问题