admin管理员组

文章数量:1289525

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

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

Share Improve this question asked Apr 9, 2015 at 9:43 hussamhussam 5945 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

You should be able to do this via:

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

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

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

本文标签: javascriptSequelize update by column it39s old valueStack Overflow