I want to run an update query on my model using the previous value in one of the fields.
This updates the model (row id=4) the 'seq' field to 5.
Model.update({
seq: 5
},{
where:{
'id':4,
}
});
Now how do I update the model to the previous value stored in the 'seq' field + 5 ?
Model.update({
seq: 'seq' + 5
},{
where:{
'id':4,
}
});
You can use
Model.update(
{ seq: sequelize.literal('seq + 5') },
{ where: { id: model_id } }
);
Or you can use the increment
method
Model.increment('seq', { by: 5, where: { id: 'model_id' }});
You can do this by using increment
Model.increment(
{ seq: +5 },
{ where: { id: 4 } }
);
and output:
UPDATE `Model` SET `seq`=`seq`+ 5 WHERE `id` = 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With