The following code is database specific:
im开发者_如何学Cport sqlalchemy
# ...
ergebnis = session.query(
my_object.attr1).filter(sa.and_(
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 an SQLAlchemy way to do this backend independent?
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.
This seems to work for Postgres, but I don't know how database-independent it is:
import sqlalchemy as sqla
...
myobject.attr2 != sqla.cast(float("NaN"), sqla.Float)
精彩评论