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! :-) On Tuesday, January 26, 2021 at 5:27:51 PM UTC-3 Diogo Baeder wrote: > 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 > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/4048d9f6-1eb3-4f20-aa78-698a6f744149n%40googlegroups.com.