Addition and Subtraction Assignment Operator With Sequelize

江枫思渺然 提交于 2019-12-17 21:15:25

问题


I would like to do an update by doing a simple addition on Sequelize.

table:

id || data
 1 ||  10 

sample:

db.table.update({ data : 1 }, { where: { id: 1 }});

after this query

id || data
 1 ||  11

I know it's a simple question, but I could not find the solution.

Which operator can I add and subtract? Thank you


回答1:


Here it is :

db.table.update({ field: Sequelize.literal('data + 1') }, { where: { id: 1 }}))

OR

User.findById(1).then(user => {
  // -----> First Way
  return user.increment('my-integer-field', {by: 2});
  // -----> Second Way
  return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
  // -----> Third Way
  return user.increment({
     'my-integer-field':    2,
     'my-very-other-field': 3
  })
});

You can also do decrement by just replacing increment with decrement.

For more detail : DO READ




回答2:


Sequelize increment & decrement

myIncrementFunc(id, incrementor) {
  User.findById(id).then(user => {
    return user.increment('tableColumnName', {by: incrementor})
  }).then(user => {})
}

Code taken form sequelize Instances tutorial for Incrementing & Decrementing: http://docs.sequelizejs.com/manual/tutorial/instances.html



来源:https://stackoverflow.com/questions/48778789/addition-and-subtraction-assignment-operator-with-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!