suppose I have the following tables (in pseudo-code)
User:
* userid
* name
Thing
* thingid
* name
* userid
Thing.user = relation(User, User.userid==Thing.userid)
Now, If I have the following query:
query =
session.query(Thing).option(joinedload('user')).join(User).filter(User.name
== 'blah')
This is going to generate a query that's similar to the following:
SELECT thing.thingid, thing.name, thing.userid, user1.userid, user1.uname
FROM thing INNER JOIN user AS user1
INNER JOIN user
WHERE user.name = 'blah'
So sqlalchemy does not eliminate the unnecessary join here, which is fine.
Is there a way for me to manually identify whether a table is already
joined in the query building process? Let me elaborate:
suppose I want to separate the code for loading child objects and filtering:
def search(child, search_by):
query = session.query(Thing)
query = load_children(query, child)
query = apply_filter(query, search_by)
return query.all()
def load_children(query, child):
return query.options(joinedload(child))
def apply_filter(query, search_by):
return query.filter(search_by)
Now, calling
search('user', User.name=='blah')
will yield the similar query above.
Is there a way for me to inspect a query object, and see what joins are
currently there, regardless of whether they're called by joinedload() or
query.join()? I know query._from_object contains the join element if a
query.join() is called, but not the case for
query.options(joinedload('user')).
Any suggestion is 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/-/_dmCom8a_6IJ.
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.