Skip to content
Advertisement

Select NULL Values in SQLAlchemy

Here’s my (PostgreSQL) table —

JavaScript

I want to select all people that are not known to be married, i.e., including those with NULL marriage_status.

This does not work —

JavaScript

Of course this does —

JavaScript

The problem is that I’m accessing it from SQLAlchemy with —

JavaScript

which gets translated to —

JavaScript

And does not work —

JavaScript

neither does —

JavaScript

How should I select NULL values through SQLAlchemy?

Advertisement

Answer

For SQLAlchemy 0.7.9 and newer Please use the answer from @jsnow. !!!

For SQLAlchemy 0.7.8 and older

(as indicated by @augurar): Because sqlalchemy uses magic methods (operator overloading) to create SQL constructs, it can only handle operator such as != or ==, but is not able to work with is (which is a very valid Python construct).

Therefore, to make it work with sqlalchemy, you should use:

JavaScript

, basically replace the is None with == None. In this case your query will be translated properly to the following SQL:

JavaScript

See IS NULL in the documentation.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement