chpolste
chpolste

Reputation: 43

sqlalchemy: null comparison commutativity

I just stumbled upon this behaviour of sqlalchemy:

>>> from sqlalchemy import Column, null

>>> print(Column("x") == null())
x IS NULL
>>> print(Column("x") == None)
x IS NULL

>>> print(null() == Column("x"))
NULL = x
>>> print(None == Column("x"))
x IS NULL

Can someone explain why == is not commutative for null() while it is when using None? What's the purpose?

Upvotes: 4

Views: 342

Answers (1)

Wondercricket
Wondercricket

Reputation: 7882

To begin, take a look at Identity vs. Equailty for an explanation on why your None conditions are both returning x IS NULL.

The reason why your null() clauses are returning different results is due to how the operators work.

As per the documentation, the operators work like the following:

In a column context, produces the clause a = b. If the target is None, produces a IS NULL.

Explanation:

Column("x") == null() # a = b -> b is NullType; returning IS NULL

null() == Column('x') # a = b -> b is ColumnType; not returning IS NULL

For more information, you may view the Sqlalchemy Docs

Upvotes: 1

Related Questions