简体   繁体   中英

updating a table in bookshelf.js

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?

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

    })

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM