Ignore duplicates in associated models when inserting rows in Sequelize

Viewed 81

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?

0 Answers
Related