问题描述
我在 mysql db 表中有一个布尔字段.
I have a boolean field in the mysql db table.
# table model
class TestCase(Base):
__tablename__ = 'test_cases'
...
obsoleted = Column('obsoleted', Boolean)
要获得所有未过时的测试用例的数量,可以简单地像这样完成:
To get the count of all the non-obsoleted test cases, that can be done simply like this:
caseNum = session.query(TestCase).filter(TestCase.obsoleted == False).count()
print(caseNum)
效果很好,但是 flake8 报告了以下警告:
That works fine, but the flake8 report the following warning:
E712:与 False 的比较应该是if cond is False:"或if not条件:"
E712: Comparison to False should be "if cond is False:" or "if not cond:"
好吧,我认为这是有道理的.所以把我的代码改成这样:
Okay, I think that make sense. So change my code to this:
caseNum = session.query(TestCase).filter(TestCase.obsoleted is False).count()
或
caseNum = session.query(TestCase).filter(not TestCase.obsoleted).count()
但是它们都不能工作.结果始终为 0.我认为过滤器子句不支持运算符是"或不是".请问有人可以告诉我如何处理这种情况.我不想禁用薄片.
But neither of them can work. The result is always 0. I think the filter clause doesn't support the operator "is" or "is not". Will someone can tell me how to handle this situation. I don't want to disable the flake.
推荐答案
那是因为 SQLAlchemy 过滤器是 == False
真正有意义的少数几个地方之一.其他地方你应该不要使用它.
That's because SQLAlchemy filters are one of the few places where == False
actually makes sense. Everywhere else you should not use it.
在该行中添加 # noqa
注释并完成.
Add a # noqa
comment to the line and be done with it.
或者你可以使用 sqlalchemy.sql.expression.false
:
Or you can use sqlalchemy.sql.expression.false
:
from sqlalchemy.sql.expression import false
TestCase.obsoleted == false()
where false()
返回会话 SQL 方言的正确值.有一个匹配的 sqlalchemy.expression.true
.
where false()
returns the right value for your session SQL dialect. There is a matching sqlalchemy.expression.true
.
这篇关于flake8 抱怨布尔比较“==";在过滤器子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!