You are right, the Person table will be used twice in the resulting SQL , but each of them has a different purpose:
- need to filter the condition:
filter(Person.is_deleted != True) - the other should look forward to loading the relationship:
options(joinedload('owner'))
But the reason your query returns incorrect results is because your filter condition is not completed. To make it a result, you must also JOIN two models:
qry = (session.query(Document). join(Document.owner). # THIS IS IMPORTANT options(joinedload(Document.owner)). filter(Person.is_deleted != True) )
This will return the correct rows, although it will still have 2 references (JOINs) in the Person table. The real solution to your request is to use contains_eager instead of joinedload :
qry = (session.query(Document). join(Document.owner). # THIS IS STILL IMPORTANT options(contains_eager(Document.owner)). filter(Person.is_deleted != True) )
van
source share