gpt4 book ai didi

node.js - 如何在 Sequelize 中忽略 SequelizeUniqueConstraintError?

转载 作者:行者123 更新时间:2023-12-03 22:19:56 27 4
gpt4 key购买 nike

使用 documentation 中的示例,一切仅在第一次插入时运行良好。在第二次及以后,出现错误。

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'});

第一个插入工作正常。

Product.create({
title: 'Chair',
tag: [
{ name: 'Alpha'},
{ name: 'Beta'}
]
}, {
include: [{
model: Tag,
as: 'tag'
}]
})

SQL日志

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 *;

后续插入 Product.create(...) 会产生错误 SequelizeUniqueConstraintError Key "(name)=(Alpha)" already exists.
如何使它在标签已经存在的情况下使用现有标签的 ID 并忽略错误?

最佳答案

我的解决方案。

添加方法 updateOrCreate

Product.updateOrCreate = function (options) {
return this.findOrCreate(options).then(res => {
let [row, created] = res;
if (created) return [row, created];
return row.update(options.defaults, options.transaction)
.then(updated => [updated, created]);
})
};
Tag.updateOrCreate = function (options) {
return this.findOrCreate(options).then(res => {
let [row, created] = res;
if (created) return [row, created];
return row.update(options.defaults, options.transaction)
.then(updated => [updated, created]);
})
};


let data = {
title: 'Chair',
tag: [
{name: 'Alpha'},
{name: 'Beta'}
]
};
return sequelize.transaction().then(t => {
return Product.updateOrCreate({
where: {title: data.title},
defaults: data,
transaction: t
}).then(res => {
let [product] = res;
return Promise.all(data.tag.map(tag => {
return Tag.updateOrCreate({
where: {name: tag.name},
defaults: tag,
transaction: t
}).then(res => {
let [tag] = res;
return Promise.resolve(tag);
}).catch(err => Promise.reject(err.message));
})).then(() => {
t.commit();
sequelize.close();
}).catch(err => Promise.reject(err));
}).catch(err => {
t.rollback();
sequelize.close();
});
});

关于node.js - 如何在 Sequelize 中忽略 SequelizeUniqueConstraintError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54866952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com