python - 为什么 peewee 没有正确嵌套这个 WHERE 子句?

标签 python mysql peewee

我正在 Python peewee ORM 中创建这样的查询:

myTableModel.records = myTableModel.select(
    myTableModel.table_field_name
    ).where(
    (myTableModel.second_field_name.is_null(True) |
    myTableModel.second_field_name == "")
    )

我可以返回通过运行 print myTableModel.records.sql() 生成的 SQL

SELECT `t1`.`table_field_name`
FROM `table_name` AS t1
WHERE (((`t1`.`second_field_name` IS NULL) OR `t1`.`second_field_name`) = '');

这没有使用正确的嵌套。这是我想要的声明:

SELECT `t1`.`table_field_name`
FROM `table_name` AS t1
WHERE `t1`.`second_field_name` IS NULL OR `t1`.`second_field_name` = '';

现有的 WHERE 子句归结为......

WHERE `t1`.`second_field_name` IS NULL = ''

...它产生的结果与我想要的完全相反,返回 second_field_namenot NULL 的所有行。

我该如何解决这个问题?我是否以某种方式错误地嵌套了 Python 代码?

最佳答案

Peewee 依靠 Python 的解析器来生成表达式。因为Python的operator precedence大多数比较都需要括号。这里没有魔法,只有 Python。

http://docs.peewee-orm.com/en/latest/peewee/querying.html?highlight=precedence#expressions

关于python - 为什么 peewee 没有正确嵌套这个 WHERE 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33905987/

相关文章:

mysql - Ruby 2 & Rails 4 查询在开发中返回结果,但在生产中返回 nil

python - 使用 Peewee 中的连接删除表中的多条记录?

peewee - 什么可以替代 playhouse.test_utils 中的 test_database () 函数?

python - urllib 模块错误!属性错误 : 'module' object has no attribute 'request'

python - 覆盖具有参数的 FastAPI 依赖项

php - 如何通过 chrome 扩展存储用户特定的数据

sqlite - 将带有子查询的SQLite查询转换为Peewee语句

python - 有没有办法用 sqlite3 在 python 中重置光标?

Python正则表达式提取 token

跨越多行的结果的 mysql 逻辑