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

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

7 months ago · Juan Pablo Isaza
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.