Using the example from the documentation, everything works well only at the first insertion. After the second and subsequent, errors appear.
const Product = this.sequelize.define('Product', {
title: Sequelize.STRING
});
const Tag = this.sequelize.define('Tag', {
name: Sequelize.STRING,
unique: true
});
const ProductTag = this.sequelize.define('ProductTag', {
product_id: Sequelize.INTEGER,
tag_id: Sequelize.INTEGER
});
Product.belongsToMany(Tag, {through: 'ProductTag', as: 'tag'});
Tag.belongsToMany(Product, {through: 'ProductTag'});
The first insert works fine.
Product.create({
title: 'Chair',
tag: [
{ name: 'Alpha'},
{ name: 'Beta'}
]
}, {
include: [{
model: Tag,
as: 'tag'
}]
})
SQL logs
INSERT INTO "Product" ("id","title","created_at","updated_at") VALUES (DEFAULT,'Chair','2019-02-25 12:51:50.802 +00:00','2019-02-25 12:51:50.802 +00:00') RETURNING *;
INSERT INTO "Tag" ("id","name","created_at","updated_at") VALUES (DEFAULT,'Alpha','2019-02-25 12:51:51.061 +00:00','2019-02-25 12:51:51.061 +00:00') RETURNING *;
INSERT INTO "Tag" ("id","name","created_at","updated_at") VALUES (DEFAULT,'Beta','2019-02-25 12:51:51.061 +00:00','2019-02-25 12:51:51.061 +00:00') RETURNING *;
INSERT INTO "ProductTag" ("product_id","tag_id","created_at","updated_at") VALUES (1,1,'2019-02-25 12:51:51.068 +00:00','2019-02-25 12:51:51.068 +00:00') RETURNING *;
INSERT INTO "ProductTag" ("product_id","tag_id","created_at","updated_at") VALUES (1,2,'2019-02-25 12:51:51.072 +00:00','2019-02-25 12:51:51.072 +00:00') RETURNING *;
Subsequent insertions Product.create(...)
produce errors SequelizeUniqueConstraintError
Key "(name)=(Alpha)" already exists.
How to make it so that if the tag already exists, it took the ID of the existing one and ignore the error?