Romeo M.
Romeo M.

Reputation: 3278

Sqlalchemy select issue with related table

I have model 'User' who belongs to model 'Role'. One 'Role' has many 'User' and one 'User' has one 'Role'.

If I want to select all users where 'Role' is hidden lets say (this is purely an example) I must do something like this:

User.query.join(Role).filter(roles_table.c.hidden==1).all()

Just by using join in this query i get the following message:

Select statement 'SELECT role.hidden FROM role, user WHERE role.id = user.role_id' returned no FROM clauses due to auto-correlation; specify correlate() to control correlation manually.

Can anyone point me to the right direction of having this query working?

Upvotes: 2

Views: 2664

Answers (2)

Denis Otkidach
Denis Otkidach

Reputation: 33200

Having User class has a relationship to Role, the simplest way would be:

User.query.filter(User.role.has(hidden=1))

Upvotes: 1

van
van

Reputation: 76992

What if you try following instead:

User.query.join(Role).filter(Role.hidden==1).all()

Upvotes: 1

Related Questions