我有以下架构,
create table test (id int primary key ,x integer)
我想用 旧值 + 新值 更新 列 x
就像使用 sequelize-nodejs 进行原始查询一样。
INSERT INTO test (id,x) VALUES (1,5)
ON DUPLICATE KEY UPDATE x=x+VALUES(x);
最佳答案
你可以这样做:
Step 1 : find instance
Step 2 : update the value with old + new
和
Step 3 : save the instance
Table.find({
where: { //condition }
})
.then(entity => {
if (entity) {
entity.val += req.body.newVal;
return entity.save()
.then(() => {
res.status(200);
})
.catch(validationError(res));
} else {
return res.status(404).end();
}
})
关于node.js - sequelize - 重复更新时批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44996566/