SQLAlchemy: filter by membership in at least one many-to-many related table

前端 未结 3 1161
小蘑菇
小蘑菇 2020-12-08 14:37

Using SQLAlchemy 0.7.1 and a MySQL 5.1 database, I\'ve got a many-to-many relationship set up as follows:

user_groups = Table(\'user_groups\', Base.metadata,         


        
相关标签:
3条回答
  • 2020-12-08 15:06

    You can use in_:

    session.query(ZKUser).filter(ZKGroup.id.in_([1,2])).all()
    
    0 讨论(0)
  • 2020-12-08 15:16

    According to the docs for any, the query will run faster if you use an explicit join instead:

    Because any() uses a correlated subquery, its performance is not nearly as good when compared against large target tables as that of using a join.

    In your case, you could do something like:

    users = (
        session.query(ZKUser)
        .join(user_groups)
        .filter(user_groups.columns.group_id.in_([1, 2, 3]))
    )
    

    This emits SQL like:

    SELECT *
    FROM users
    JOIN user_groups ON users.id = user_groups.user_id 
    WHERE user_groups.group_id IN (1, 2, 3)
    
    0 讨论(0)
  • 2020-12-08 15:20

    OK, after a lot of research, I realized that it was my own ignorance of SQL terminology that was holding me back. My search for a solution to find users belonging to "at least one of" the list of groups should have been to find users belonging to "any" of the list of groups. The any ORM function from SQLAlchemy does exactly what I needed, like so:

    session.query(ZKUser).filter(ZKUser.groups.any(ZKGroup.id.in_([1,2,3])))
    

    That code emits this SQL (on MySQL 5.1):

    SELECT * FROM users 
    WHERE EXISTS (
        SELECT 1 FROM user_groups, groups 
        WHERE users.id = user_groups.contact_id 
            AND groups.id = user_groups.group_id 
            AND groups.id IN (%s, %s, %s)
        )
    
    0 讨论(0)
提交回复
热议问题