mysql - 序列化更新并扣除具有不同 id 的多行

标签 mysql orm sequelize.js

我有一个产品表,需要使用不同的 id 一次更新多行。尽管如此,我仍然不知道如何实现。

module.exports = sequelize.define('products', {

  product_id: {
    type: Sequelize.INTEGER(20),
  },
  product_quantity: {
    type: Sequelize.INTEGER(225),
  },  
})

var cart_list = [
  { id: 1, curr_quantity: 1 },
  { id: 2, curr_quantity: 4 },
  { id: 3, curr_quantity: 5 },
  { id: 4, curr_quantity: 2 }
]

Product.update({
  product_quantity
})
.then(product => {

})

最佳答案

正如我检查过您的代码一样,您有带有多个 id 和不同数量的 car_list 数组,因为我们使用循环和 promise 。

  module.exports = sequelize.define('products', {    
         product_id: {
            type: Sequelize.INTEGER(20),
         },
         product_quantity: {
            type: Sequelize.INTEGER(225),
         },  
    });

    let promises = [];
    var cart_list = [
        { id: 1, curr_quantity: 1 },
        { id: 2, curr_quantity: 4 },
        { id: 3, curr_quantity: 5 },
        { id: 4, curr_quantity: 2 }
    ]

    cart_list.forEach(element => {
        promises.push(Product.update({product_quantity : element.curr_quantity},{where:{'product_id' : element.id}}))
    });

    Promise.all(promises).then(resultdata=> {
        console.log('Success-------->',resultdata)
    }).catch(err =>{        
        console.log('Failed---------------->',err);
    });

关于mysql - 序列化更新并扣除具有不同 id 的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57486836/

相关文章:

mysql - 用于更新表中的行、插入/更新同一个表的 SQL 语句

mysql docker容器经常崩溃

mysql - Symfony 4 SQLSTATE [42000] 指定的 key 太长

java - 为什么要使用Hibernate Query addScalar方法?

javascript - 添加和新列/属性以 Sequelize 模型

php - Codeigniter插入失败

mysql - 更新查询在工作台中中断?

java - 使用 Hibernate 持久化应用程序设置

node.js - SequelizeJS,这是使用此模型创建此 JSON 结果的最佳方法吗

javascript - 将时间戳与 Sequelize 查询中的日期进行比较