Accessing filtered attributes not working

12 views
Skip to first unread message

Diogo Baeder

unread,
Jan 26, 2021, 3:27:51 PM1/26/21
to sqlalchemy
Hi guys,

I may be misunderstanding how SQLAlchemy works in this regard, but somehow I can't figure out how to do a joined query for ORM objects in a way that I can filter on related children objects and have them available (filtered) as attributes.

Take this snippet for example: https://pastebin.com/dACLYpYe - if I run it, I expect it to print me the company name ("ACME") and only one of the users ("Bob"), who is the active user. However, it's also bringing the other user saved in the database, "Alice", who is an inactive user.

How can I make this work in such a way that I can access the main object I need, which is the company, and then loop through the users that belong to it, but respecting the filtering applied to the query? In the printed SQL statement the query is correct, but the results I get in my Python code are not, no matter if I flush and expire all objects before doing the query.

Any ideas?

Thanks in advance!
Diogo

Diogo Baeder

unread,
Jan 26, 2021, 3:50:08 PM1/26/21
to sqlalchemy
Sorry, nevermind, found it! "contains_eager" does the trick. I never used it before, and I thought it was just a more complicated way of doing what "joinedload" already does, but it's clearly not.

Thanks anyway! :-)
Reply all
Reply to author
Forward
0 new messages