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
    • Calculadora

0

203
Vistas
Ignore duplicates in associated models when inserting rows in Sequelize

Let's assume there are two tables, cars and manufacturers. The cars table has a foreign key reference to the manufacturers table. Furthermore, the cars table has a unique key constraint on the model field and the manufacturers table has one on the name field.

In Sequelize, I can create a new instance of a car and automatically have it create the manufacturer instance and link the two in a single call.

Car.create({
    model: "i4",
    Manufacturer: {
        name: "BMW",
    },
}, {
    include: [Manufacturer],
});

This works fine if the unique key constraints are not violated in either table. Adding ignoreDuplicates: true solves the unique key violation issue for the cars table but not for the manufacturers table.

How do I make this query behave so that if the manufacturer exists, it only inserts the car row, and if both of them exist, it does nothing?

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos