在SQLAlchemy中选择NULL值
这是我的(PostgreSQL)表格 --
test=> create table people (name varchar primary key,
marriage_status varchar) ;
test=> insert into people values ('Ken', 'married');
test=> insert into people values ('May', 'single');
test=> insert into people values ('Joe', NULL);
我想选择所有不被认为已婚的人,也就是说,包括那些婚姻状态为NULL的人。
这个方法不有效 --
test=> select * from people where marriage_status != 'married' ;
name | marriage_status
------+-----------------
May | single
(1 row)
当然,这个方法有效 --
test=> select * from people where marriage_status != 'married'
or marriage_status is NULL ;
name | marriage_status
------+-----------------
May | single
Joe |
问题是我通过SQLAlchemy来访问它,使用的是 --
...filter(or_(people.marriage_status!='married',
people.marriage_status is None))
这段代码被转换成了 --
SELECT people.name as name,
people.marriage_status as marriage_status
FROM people
WHERE people.marriage_status != %(status_1)s OR False
sqlalchemy.engine.base.Engine.... {'status_1': 'married'}
而这个方法不有效 --
test=> select * from people where marriage_status != 'married'
or False;
name | marriage_status
------+-----------------
May | single
(1 row)
这个方法也不行 --
test=> select * from people where marriage_status != 'married'
or NULL;
name | marriage_status
------+-----------------
May | single
(1 row)
我应该如何通过SQLAlchemy选择NULL值呢?
6 个回答
29
一种优雅的写法是使用 is_ 和 is_not,下面的例子展示了这一点:
query.filter(people.student.is_not(None))
query.filter(people.student.is_(None))
188
对于SQLAlchemy 0.7.9及更新版本,请参考@jsnow的回答。!!!
对于SQLAlchemy 0.7.8及更早版本
(正如@augurar所指出的):因为sqlalchemy使用了魔法方法(运算符重载)来创建SQL
结构,所以它只能处理像!=
或==
这样的运算符,而无法处理is
(这是一个非常有效的Python结构)。
因此,为了让它在sqlalchemy中正常工作,你应该使用:
...filter(or_(people.marriage_status!='married', people.marriage_status == None))
也就是说,把is None
替换成== None
。这样你的查询就会正确转换成以下SQL:
SELECT people.name AS people_name, people.marriage_status AS people_marriage_status
FROM people
WHERE people.marriage_status IS NULL OR people.marriage_status != ?
可以查看文档中的IS NULL
部分,链接在这里:文档。