我正在 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_name
为 not 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/