TAGS :Viewed: 1 - Published at: a few seconds ago

[ Is there a Database independent way with SQLAlchemy to query filtered by "None"/"NaN"? ]

The following code is DB specific:

import sqlalchemy
# ...
ergebnis = session.query(
        my_object.attr2 != 'NaN')).all() # PostgreSQL
        my_object.attr2 != None)).all() # sQLite

With PostgreSQL it is "'NaN'", with SQLite "None" (without single quotes). Is there a SQLAlchemy-way to do this backend independant?

Answer 1

If you want to compare against the 'NaN' ("not a number") float value, then do an explicit cast to float: float('NaN'). In this case SQLAlchemy should do the same conversion.