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:
SELECT thing.thingid, thing.name, thing.userid, user1.userid, user1.name
FROM thing INNER JOIN user AS user1
INNER JOIN user
WHERE user.name == 'blah'
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.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/oFfq3pWQm5wJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.