首页 文章

sequelize - 重复更新时批量插入

提问于
浏览
0

我有以下架构,

create table test (id int primary key ,x integer)

我想用 old + new value 更新重复 column x

喜欢跟随sequelize-nodejs的原始查询 .

INSERT INTO test (id,x) VALUES (1,5)
  ON DUPLICATE KEY UPDATE x=x+VALUES(x);

1 回答

  • 0

    你可以这样做:

    步骤1:查找实例步骤2:使用旧的更新值

    第3步:保存实例

    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();
              }
            })
    

相关问题