I want to avoid double joining on the same table. I know query._from_obj is where the query stores the join elements. However, it's not there if the join is from query.options(joinedload('some_relation')). For example, I have the following table relations:
User:
* userid
* name
Thing
* thingid
* name
* userid
Thing.user = relation(User, User.userid==Thing.userid)
If I call:
query = session.query(Thing).options(joinedload('user')).filter(User.name=='blah').all()
This will generate the following query:
FROM thing INNER JOIN user AS user1
INNER JOIN user
Notice the double join there.
Now, I wouldn't do that if I'm writing the query in a single function, but if the code is modular, the child object loading and filtering is done in separate functions, with the query being passed around. Is there a way for me to detect whether a query already has a join on a certain table, whether the join is from query.join() or query.options(joinedload(x))?
Any suggestion is welcome and appreciated.