On 08/29/2016 08:32 AM, [email protected] wrote:
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?

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

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

Reply via email to