Hello.

On 31.10.2014 18:33, Simon King wrote:
> At a guess, I would say that the Python code "Foo.bar != bar" means 
> that you are looking for Foo objects that aren't related to bar.
> This includes Foos that are related to a *different* Bar instance,
> as well as Foos that aren't related to *any* bar instance. In sql
> terms, foo.bar_id != bar.id OR foo.bar_id is NULL.
> 
> Whereas in the other instance, you've asked for something more
> specific, so that's what SA has given you.

Thank you for the explanation. Seems logical. I have just never looked at it
this way. The generated SQL has surprised me because in my model:
* Foo.bar cannot be NULL by definition and
* bar instance (the query parameter used in the filter()) already has an id.

Also, the SQL SELECT with an OR condition might confuse the DB query planner to
choose a suboptimal plan.

I have learned new lesson: do not use SA object references when the ultimate
goal is to create a SQL SELECT.


Thank you once again to both of you,

Ladislav Lenart

PS: Sorry for the delay. I was offline during the weekend.


>> On 31 Oct 2014, at 16:36, Ladislav Lenart <lenart...@volny.cz> wrote:
>>
>> Hello.
>>
>> I have just noticed (by accident) a semantic difference of the not-equal
>> operator (!=), depending on the style used. For example a query
>>
>>    session.query(Foo).filter(Foo.bar_id != bar.id)
>>
>> produces SQL like (expected)
>>
>>    SELECT *
>>    FROM foo
>>    WHERE foo.bar_id != %(bar_id_1)s
>>
>> whereas a query
>>
>>    session.query(Foo).filter(Foo.bar != bar)
>>
>> produces SQL like (NOT expected)
>>
>>    SELECT *
>>    FROM foo
>>    WHERE (
>>        foo.bar_id != %(bar_id_1)s
>>        OR foo.bar_id IS NULL
>>    )
>>
>> I thought that
>>
>>    Foo.bar != bar
>>
>> is just a shortcut for
>>
>>   Foo.bar_id != bar.id
>>
>> Is this difference intentional?
>> May I know why (i.e. the motivation)?
>>
>>
>> Thanks in advance,
>>
>> Ladislav Lenart


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

Reply via email to