[英]SQLAlchemy not equals on postgres
I was going to log an issue on the SQLAlchemy site, but before I did I just wanted to check I hadn't missed anything. 我本打算在SQLAlchemy网站上记录一个问题,但是在我这样做之前,我只想检查一下我是否没有遗漏任何东西。 I have asked on IRC and I think this could possibly be a legit issue. 我曾问过IRC,我认为这可能是一个合法的问题。
What it boils down to is the != filters are not taking into account empty/NULL cells. 归结为!=过滤器未考虑空/ NULL单元。
So lets say for instance 因此,举例来说
Session.query(Table).\
filter(Table.column != 1).all()
Now I would expect this to return all records where the value was not equal to 1. However, what it actually returns is a list of records that have data in, that isn't equal to 1. 现在,我希望它返回值不等于1的所有记录。但是,它实际返回的是包含数据的记录列表,该记录不等于1。
So if for example column was a NULL/empty value, it would not be returned by this query, to which I (and others) would expect it to be. 因此,例如,如果column是NULL /空值,那么此查询将不会返回它(我(和其他人)希望它返回)。
Now obviously it's best practice to use NOT NULL on creating columns, but I just wanted to see if there was a fix for this, or whether its worth filing a bug 现在显然是在创建列时使用NOT NULL的最佳实践,但是我只是想看看是否有针对此的修复程序,或者是否值得提交错误
Well, this has nothing to do with SQLAlchemy, it is just how NULL rolls. 好吧,这与SQLAlchemy无关,这只是NULL滚动的方式。 NULL != 1
is neither true or false, it evaluates to NULL. NULL != 1
既不是true也不是false,它的计算结果为NULL。 Thus, the WHERE clause won't return any row with NULL value for the column. 因此,WHERE子句不会为该列返回任何具有NULL值的行。
You can go with either: 您可以选择以下两者之一:
Session.query(Table).filter(or_(
Table.column == None,
Table.column != 1,
))
or something like: 或类似的东西:
Session.query(Table).filter(
func.coalesce(Table.column, -1) != 1)
to transform NULL values into something usable for comparison, as -1 != 1
evaluates to true. 将NULL值转换为可用于比较的值,因为-1 != 1
计算为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.