sequelize 查找空字符串报错
sequelize 查找空字符串报错
需求:找出为""或者为null的行数
错误版本
const count = await Model.count({
where: {
$or: [
{ value: null },
{ value: '' },
],
},
});
sql: SELECT count(*) AS `count` FROM `setting` AS `setting` WHERE ((`setting`.`deleted_at` > '2019-04-16 10:02:39' OR `setting`.`deleted_at` IS NULL) AND (`setting`.`required` = true AND (`setting`.`value` IS NULL OR `setting`.`value` = '\"\"')));
- 原因:nodejs sequelize库防注入测试,Model.count把’‘转义了’""’
正确版本
const count = await this.app.model.Setting.count({
where: {
$or: [
{ value: null },
literal('value = ""'),
],
},
});
1 回复
let query = {}; if (ctx.query.value) { query[$or].push({value: ctx.query.value}) } const count = await this.app.model.Setting.count({ where: query }); 这样写是不是更好点?