Sequelize, how do I get current value of updating row?

278 views
Skip to first unread message

bdog10123

unread,
Jun 19, 2015, 6:38:43 PM6/19/15
to sequ...@googlegroups.com

How do I get the current value of the row I'm about to update? Do I have to do a findOne query before I do an update, in order to find the previous value? Here's what I want to do:

UPDATE userModels SET nonce = nonce + 1 WHERE u_id = 1

Is there some way to make that work from here? How would I get [CURRENT VALUE OF NONCE]? Do I have to do a separate query?

return userModels.update({
        nonce: [CURRENT VALUE OF NONCE] + 1
   
},{
       
where: {
            u_id
: 1
       
}
   
}
);


Mick Hansen

unread,
Jun 21, 2015, 4:08:36 AM6/21/15
to bdog10123, sequ...@googlegroups.com
If you want the specific value of nonce you have to do a find, but what you really want is an atomic query.
So try {nonce: sequelize.literal('nonce + 1')} 
--
Mick Hansen
@mhansendev
mhansen.io
Reply all
Reply to author
Forward
0 new messages