• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

260
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?

about 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda