Reputation: 331
I am trying to updated a status field in my table using this mysql query:
UPDATE battles SET status = "FINISHED" WHERE status like "LIVE" AND end_date < NOW();
However I am using bookshelf.js and can't seem to make it work. That's how it looks like:
return this.Model.collection().query(qb => qb.where('status', 'LIKE', status).andWhere('end_date', '<', connection.knex.fn.now())).set({ status: 'FINISHED' });
Could you guys tell me what I am doing wrong or how to do it correctly?
Upvotes: 0
Views: 1020
Reputation: 3194
You are using set method to update the column of the table. But Check the official docs, set method is used to update the collection of models. Example:
var vanHalen = new bookshelf.Collection([eddie, alex, stone, roth]);
vanHalen.set([eddie, alex, stone, hagar]);
You can use the following structure
CollectionModel.forge().query({where: {status: "LIVE"}}).fetch().then(function (resData) {
_.each(resData.models, function (model) { //I am looping over models using underscore, you can use any loop
model.save({ status: 'FINISHED' }, {method: 'update'}, {patch: true})
.then(function(row) {
console.log("row updated");
})
})
})
Upvotes: 1