hussam
hussam

Reputation: 594

Sequelize: update by column it's old value

i am trying to update a set of data using Sequelize by this query

Users.update({
    flag: 'flag & ~ 2'
} , {
    where : {
        id :{
            gt : 2
        }
    }
})

the generated query is

UPDATE `users` SET `flag`='flag & ~ 2' WHERE id > 2

But is should be

UPDATE `users` SET `flag`=flag & ~ 2 WHERE id > 2

so my question is how can i update data by by it's old value

Regards

Upvotes: 6

Views: 5546

Answers (3)

Siddharth Sunchu
Siddharth Sunchu

Reputation: 1094

You can do that with sequelize literal.

model.update({'count': sequelize.literal('count + 1')}, { where: { id: 1 }})

You can also do it with the increment method where by indicates from what value you want to increment.

Model.increment('seq', { by: 5, where: { id: 'model_id' });

Upvotes: 0

Matthias Dietrich
Matthias Dietrich

Reputation: 521

You should be able to do this via:

Users.update({
    flag: sequelize.literal('flag & ~ 2')
} , {
    where : {
        id :{
            gt : 2
        }
    }
});

Upvotes: 10

hexerei software
hexerei software

Reputation: 3160

Simplest solution would be by using raw queries:

sequelize.query("UPDATE users SET flag=flag & ~ 2 WHERE id > 2").spread(function(results, metadata) {
  // Results will be an empty array and metadata will contain the number of affected rows.
})

Upvotes: 0

Related Questions