Sequelize.js删除查询?

lak*_*nen 81 javascript node.js sequelize.js

有没有办法写一个像findAll这样的delete/deleteAll查询?

例如,我想做这样的事情(假设MyModel是Sequelize模型......):

MyModel.deleteAll({ where: ['some_field != ?', something] })
    .on('success', function() { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

nck*_*lvn 192

对于使用Sequelize版本3及更高版本的任何人,请使用:

Model.destroy({
    where: {
        // criteria
    }
})
Run Code Online (Sandbox Code Playgroud)

Sequelize文档 - Sequelize教程

  • 很公平; 虽然因为这是Google上的第一个搜索结果,并且人们也不鼓励提出已经被问过的问题,但似乎接受的答案应该得到更新...但这可能更多是一个网站范围的问题. (2认同)

ale*_*lex 19

我深入研究了代码,一步一步地进入以下文件:

https://github.com/sdepold/sequelize/blob/master/test/Model/destroy.js

https://github.com/sdepold/sequelize/blob/master/lib/model.js#L140

https://github.com/sdepold/sequelize/blob/master/lib/query-interface.js#L207-217

https://github.com/sdepold/sequelize/blob/master/lib/connectors/mysql/query-generator.js

我找到了什么:

没有deleteAll方法,你可以在记录上调用destroy()方法,例如:

Project.find(123).on('success', function(project) {
  project.destroy().on('success', function(u) {
    if (u && u.deletedAt) {
      // successfully deleted the project
    }
  })
})
Run Code Online (Sandbox Code Playgroud)

  • destroy()不在sequelizejs.com的文档中,以防其他人在这里寻找像我一样 (3认同)
  • 您的链接都为我返回了404。我是唯一一个? (2认同)

cgi*_*omi 15

不知道问题是否仍然相关,但我在Sequelize的文档中找到了以下内容.

User.destroy('`name` LIKE "J%"').success(function() {
    // We just deleted all rows that have a name starting with "J"
})
Run Code Online (Sandbox Code Playgroud)

http://sequelizejs.com/blog/state-of-v1-7-0

希望能帮助到你!

  • 作为参考,这在[lib/model.js](https://github.com/sequelize/sequelize/blob/51ce2fb7f60359fc04814afe6779bb0ada41b18d/lib/model.js#L1272)中定义,您不必使用字符串.你可以使用任何类型的`where`对象(例如`{someId:123}`). (2认同)

His*_*ffa 9

此示例显示如何承诺而不是回调.

Model.destroy({
   where: {
      id: 123 //this will be your id that you want to delete
   }
}).then(function(rowDeleted){ // rowDeleted will return number of rows deleted
  if(rowDeleted === 1){
     console.log('Deleted successfully');
   }
}, function(err){
    console.log(err); 
});
Run Code Online (Sandbox Code Playgroud)

查看此链接了解更多信息 http://docs.sequelizejs.com/en/latest/api/model/#destroyoptions-promiseinteger

  • 这不再像那样工作。返回的是受影响的行 ID/而不是受影响的行数。 (2认同)

Adi*_*iii 5

在新版本中,您可以尝试这样的操作

function (req,res) {    
        model.destroy({
            where: {
                id: req.params.id
            }
        })
        .then(function (deletedRecord) {
            if(deletedRecord === 1){
                res.status(200).json({message:"Deleted successfully"});          
            }
            else
            {
                res.status(404).json({message:"record not found"})
            }
        })
        .catch(function (error){
            res.status(500).json(error);
        });
Run Code Online (Sandbox Code Playgroud)


li *_*i x 5

这是一个使用 Await / Async 的 ES6 示例:

    async deleteProduct(id) {

        if (!id) {
            return {msg: 'No Id specified..', payload: 1};
        }

        try {
            return !!await products.destroy({
                where: {
                    id: id
                }
            });
        } catch (e) {
            return false;
        }

    }
Run Code Online (Sandbox Code Playgroud)

请注意,我!!在 await 的结果上使用Bang Bang 运算符,这会将结果更改为布尔值。