Brian Smith
Brian Smith

Reputation: 145

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

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
        }
    }
);

Upvotes: 1

Views: 1649

Answers (2)

Ashvin Ahjolia
Ashvin Ahjolia

Reputation: 31

Please try this, it works for me :

 return userModels.update(
     { nonce: sequelize.literal('nonce + 1') },
     { where: { u_id: 1} }
 );

Upvotes: 1

Dan Rocha
Dan Rocha

Reputation: 635

As docs say, the updated value of an object should be retrieved again. @LJ_1102 commented this link but here on the API implementation you can see a better explanation.

Refer to reload if you cant to reload an instance and prevent creating it again

Upvotes: 0

Related Questions