Home > OS >  Sequelize .update() not using the correct where and updates all rows
Sequelize .update() not using the correct where and updates all rows

Time:12-20

I'm trying to create an approval command.
Everything about it, should be working, but it never works correctly.
I've changed my Database to have "id" first, but it still prefers to update on "user" instead.

I've tried having my code just be; but that never worked out. Even though I use it everywhere else.

await ticketApproval.update({status: 1});

I've checked documentation and other StackOverflow questions and answers.
Nothing has worked yet, but from what I can tell, nothing in my code should be telling it to update on the "user" id. Since I only ever tell it to search on the ticket "id" and "server" id
It feels like it's just outright ignoring the Where.

Code
Code

Console
enter image description here

Test Sample
enter image description here

CodePudding user response:

You clearly confuse the update method of a record instance with the static update of a model. Only static update has the where option because it updates several records in DB using passed conditions in contrast to the instance update method that uses a primary key value of a certain record to update it with passed values.
Compare:

// here we have a model instance for a concrete record
const ticketApproval = await Ticket.findOne({
  where: {
     id: ticketToApprove
  }
})
// by calling the instance `update` we always update this concrete record only
// using a primary key value condition, see a primary key field defined in a model
await ticketApproval.update({ status: 1 });

with

// by calling the static `update` we update all records that satisfy the indicated conditions
await Ticket.update({ status: 1 }, {
  where: {
     id: ticketToApprove
  }
});

  • Related