sqlalchemy IS NOT NULL select

前端 未结 3 1850
走了就别回头了
走了就别回头了 2020-12-23 13:00

How can I add the filter as in SQL to select values that are NOT NULL from a certain column ?

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;


        
相关标签:
3条回答
  • 2020-12-23 13:35

    Starting in version 0.7.9 you can use the filter operator .isnot instead of comparing constraints, like this:

    query.filter(User.name.isnot(None))

    This method is only necessary if pep8 is a concern.

    source: sqlalchemy documentation

    0 讨论(0)
  • 2020-12-23 13:38

    In case anyone else is wondering, you can use is_ to generate foo IS NULL:

    >>> from sqlalchemy.sql import column
    >>> print column('foo').is_(None)
    foo IS NULL
    >>> print column('foo').isnot(None)
    foo IS NOT NULL
    
    0 讨论(0)
  • 2020-12-23 13:43

    column_obj != None will produce a IS NOT NULL constraint:

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

    or use isnot() (new in 0.7.9):

    Implement the IS NOT operator.

    Normally, IS NOT is generated automatically when comparing to a value of None, which resolves to NULL. However, explicit usage of IS NOT may be desirable if comparing to boolean values on certain platforms.

    Demo:

    >>> from sqlalchemy.sql import column
    >>> column('YourColumn') != None
    <sqlalchemy.sql.elements.BinaryExpression object at 0x10c8d8b90>
    >>> str(column('YourColumn') != None)
    '"YourColumn" IS NOT NULL'
    >>> column('YourColumn').isnot(None)
    <sqlalchemy.sql.elements.BinaryExpression object at 0x104603850>
    >>> str(column('YourColumn').isnot(None))
    '"YourColumn" IS NOT NULL'
    
    0 讨论(0)
提交回复
热议问题