Sequelize .update() 不使用正确的位置并更新所有行
Sequelize .update() not using the correct where and updates all rows
我正在尝试创建批准命令。
关于它的一切,应该工作,但它永远不会正常工作。
我已将我的数据库更改为首先具有“id”,但它仍然更喜欢更新“user”。
我试过让我的代码成为;但这从来没有成功过。即使我在其他任何地方都使用它。
await ticketApproval.update({status: 1});
我已经检查了文档和其他 Whosebug 问题和答案。
还没有任何效果,但据我所知,我的代码中没有任何内容应该告诉它更新 "user" id。因为我只告诉它搜索票 "id" 和 "server" id
感觉完全忽略了Where。
代码
控制台
测试样本
您显然混淆了记录实例的 update
方法与模型的静态 update
方法。
只有静态 update
具有 where
选项,因为它使用传递的条件更新数据库中的多个记录,而实例 update
方法使用特定记录的主键值来更新它传递值。
比较:
// here we have a model instance for a concrete record
const ticketApproval = await Ticket.findOne({
where: {
id: ticketToApprove
}
})
// by calling the instance `update` we always update this concrete record only
// using a primary key value condition, see a primary key field defined in a model
await ticketApproval.update({ status: 1 });
与
// by calling the static `update` we update all records that satisfy the indicated conditions
await Ticket.update({ status: 1 }, {
where: {
id: ticketToApprove
}
});
我正在尝试创建批准命令。
关于它的一切,应该工作,但它永远不会正常工作。
我已将我的数据库更改为首先具有“id”,但它仍然更喜欢更新“user”。
我试过让我的代码成为;但这从来没有成功过。即使我在其他任何地方都使用它。
await ticketApproval.update({status: 1});
我已经检查了文档和其他 Whosebug 问题和答案。
还没有任何效果,但据我所知,我的代码中没有任何内容应该告诉它更新 "user" id。因为我只告诉它搜索票 "id" 和 "server" id
感觉完全忽略了Where。
代码
控制台
测试样本
您显然混淆了记录实例的 update
方法与模型的静态 update
方法。
只有静态 update
具有 where
选项,因为它使用传递的条件更新数据库中的多个记录,而实例 update
方法使用特定记录的主键值来更新它传递值。
比较:
// here we have a model instance for a concrete record
const ticketApproval = await Ticket.findOne({
where: {
id: ticketToApprove
}
})
// by calling the instance `update` we always update this concrete record only
// using a primary key value condition, see a primary key field defined in a model
await ticketApproval.update({ status: 1 });
与
// by calling the static `update` we update all records that satisfy the indicated conditions
await Ticket.update({ status: 1 }, {
where: {
id: ticketToApprove
}
});