Skip to content
Advertisement

How to select filtering by associated entries collection in sqlalchemy

I have many-to-many relationships for users and roles and I want to select user which have specific roles using realtions. For example I want to get user having:

JavaScript

so I tried

JavaScript

(where roles – List[Roles])

but I got

JavaScript

then I tried

JavaScript

where roles already List[str]

And I got

JavaScript

but any() selects entry that has any associated role when I need entry that has all required roles. So how to select it right way using relationships instead of joins and etc.?

JavaScript

Advertisement

Answer

I didn’t find what I was looking for, so for now I just wrote it with joins:

JavaScript

where roles_ids was collected from Roles table before. And if you need user with only required roles you can replace “>=” with “==”.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement