开发者

Is there a database independent way to filter by "None"/"NaN"?

开发者 https://www.devze.com 2023-02-18 22:46 出处:网络
The following code is database specific: im开发者_如何学Cport sqlalchemy # ... ergebnis = session.query(

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消