joinedload() vs join()

2,288 views
Skip to first unread message

mgoz...@starfishstorage.com

unread,
Aug 29, 2016, 8:32:24 AM8/29/16
to sqlalchemy
Hi

I have a question about the difference between .join() and .options(joinedload()).

I'm using example from http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/hybrid.html#working-with-relationships (the one with SavingsAccount and User)
The doc shows SQL for

Session().query(User, User.balance).join(User.accounts).filter(User.balance > 5000)

which is

SELECT "user".id AS user_id, "user".name AS user_name, account.balance AS account_balance
FROM "user" JOIN account ON "user".id = account.user_id
WHERE account.balance > :balance_1


if I change the Python code to

Session().query(User, User.balance).options(joinedload(User.accounts)).filter(User.balance > 5000))

it generates different SQL, which I believe does a cartesian join:

SELECT "user".id AS user_id, "user".name AS user_name, account.balance AS account_balance, account_1.id AS account_1_id, account_1.user_id AS account_1_user_id, account_1.balance AS account_1_balance
FROM account, "user" LEFT OUTER JOIN account AS account_1 ON "user".id = account_1.user_id
WHERE account.balance > :balance_1

Is there something I'm missing?

Mike Bayer

unread,
Aug 29, 2016, 10:45:39 AM8/29/16
to sqlal...@googlegroups.com
joinedload() only refers to collections that are fully loaded on User
and aren't part of what you can manipulate in the query. Take a look at
http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html#the-zen-of-eager-loading
.





>
> --
> 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+...@googlegroups.com
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Marcin Gozdalik

unread,
Sep 13, 2016, 7:31:12 AM9/13/16
to sqlal...@googlegroups.com
Thanks for the link. It seems I missed that.

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/ACO-Jhdud3w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages