sequelize 查找空字符串报错
发布于 6 年前 作者 18820227745 3920 次浏览 来自 分享

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` = '\"\"')));

正确版本

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 }); 这样写是不是更好点?

回到顶部