Sequelize update does not work anymore: “Missing where attribute in the options parameter passed to update”

人走茶凉 提交于 2019-12-06 01:05:32

问题


The official API documentation suggests using Model.update like this:

var gid = ...;
var uid = ...;

var values = { gid: gid };
var where = { uid: uid };
myModel.update(values, where)
.then(function() {
    // update callback
});

But this gives me: "Missing where attribute in the options parameter passed to update". The docs also mention that this usage is deprecated. Seeing this error makes me think, they already changed it. What am I doing wrong?


回答1:


Apparently, the docs have not been updated yet. But the table's where row of the Model.update API docs suggests prefixing your selection with where, like so:

var gid = ...;
var uid = ...;

var values = { gid: gid };
var selector = { 
  where: { uid: uid }
};
myModel.update(values, selector)
.then(function() {
    // update callback
});

And it works!

UPDATE:

The docs have since been updated (and the docs have also been moved). Check out Model.update on docs.sequelize.com. Note that options.where is not optional (it is not in brackets []).




回答2:


To update all the rows at once, provide a where with an empty object. As the code bellow show:

await DisplayMediaSequence.update({
    default: false
}, {
    where: {}, // <--- here
    transaction
});

await DisplayMediaSequence.update({
    default: true
}, {
    where: {
        id
    },
    transaction
});


来源:https://stackoverflow.com/questions/26581715/sequelize-update-does-not-work-anymore-missing-where-attribute-in-the-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!