1

I want to update the column qty like this

qty = qty + x

My code is like this

db.Stock.update({
            qty: Sequelize.col('Stock.qty') + element.qty
        }, {
            where: {
                item_id: element.item_id
            }
        })

and my model, I have this also

{
    hooks: {
      beforeUpdate: function (Stock, options) {
        console.log("Stock", Stock._previousDataValues.qty);
      },
    }
  }

but this gives me the error

UPDATE `Stocks` SET `qty`='[object Object]10',`updatedAt`='2018-08-11 13:51:18' WHERE `item_id` = 8

I'm new to the sequelize and how can I successfully do this?

Ersoy
  • 8,816
  • 6
  • 34
  • 48
margherita pizza
  • 6,623
  • 23
  • 84
  • 152

1 Answers1

2

All you need is literal , try the below way :

db.Stock.update({ 
        qty: Sequelize.literal(`qty + ${element.qty}`) // <---- HERE
    }, 
    { where: { item_id: element.item_id }
);

For More : Do Read

Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122