Javascript Sequelize Where 如果不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50646216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:39:29  来源:igfitidea点击:

Sequelize Where if not null

javascriptsqlnode.jsexpress

提问by Asfgasdf

Lets say I want to do a select command with

假设我想做一个选择命令

WHERE ID=2134

WHERE ID=2134

Butif the user does not provide the id then it should just not bother with the WHERE ID (since it is null)

但是如果用户不提供 id 那么它就不应该打扰 WHERE ID(因为它是空的)

How can I handle this with Sequelize?

我如何用 Sequelize 处理这个问题?

回答by chetan palrecha

Post.update({
  updatedAt: null,
}, {
  where: {
    deletedAt: {
      [Op.ne]: null
    }
  }
});

回答by Yurgen

Basically your query should have

基本上你的查询应该有

WHERE id=1234 OR id is null

so for sequilize it would

所以对于sequilize它会

const Op = Sequelize.Op;

Model.findAll({
   where: {
      id: {
        [Op.or]: [1234, null]
      }
   }
});

Here's the docs: http://docs.sequelizejs.com/manual/tutorial/querying.html#operators

这是文档:http: //docs.sequelizejs.com/manual/tutorial/querying.html#operators