Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos