node.js - sequelize - 重复更新时批量插入

标签 node.js orm sequelize.js

我有以下架构,

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/

相关文章:

node.js - Meteor 中间件和服务器端路由

node.js - 生成按字典顺序升序的唯一 ID

node.js - 如何在 Node.js 中保存文件?

java - JPA 与 JDBC、存储过程和 Co. 或如何说服老派程序员尝试 ORM?

postgresql - 如何使用 Sequelize 实现 PostgresQL tsvector 进行全文搜索?

node.js - 与 nginx、nodejs 和 socket.io 的 WebSocket 连接失败

java - 使用 Oracle 10g 时 Hibernate 的浮点列模式验证已知问题的最佳解决方法是什么?

c# - 如何在 asp.net MVC 中映射 2 个不同的列表

node.js - Sequelize ORM中HasOne和BelongsTo的区别

mysql - Sequelize - 如何在一个种子文件中创建多个记录