Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

352
Views
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 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!