Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

152
Views
Sequelize .update() not using the correct where and updates all rows

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

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.