How to update column value with existing column value in sequelize

7,055 views
Skip to first unread message

胡雪亮

unread,
Jan 5, 2015, 11:00:53 PM1/5/15
to sequ...@googlegroups.com

like:

update user.age = user.age+1 where id=123

try:

models.User.update({age:sequelize.col('age')+1},{where:{id:123}}).success()...

Mick Hansen

unread,
Jan 6, 2015, 2:25:34 AM1/6/15
to 胡雪亮, sequ...@googlegroups.com
We actually have increment/decrement functions for atomic counters: http://sequelize.readthedocs.org/en/latest/api/instance/#incrementfields-options-promise

Other than you should be able to do something like:

```js
models.User.update({age: sequelize.literal('age + 1')}, {where: {id: 123}})
```
--
Mick Hansen
@mhansendev
mhansen.io

Chengshu Yu

unread,
Apr 28, 2016, 4:52:32 PM4/28/16
to Sequelize
幸亏你post了query!要不然我还要找好久!This works!
Reply all
Reply to author
Forward
0 new messages