我是 MySQL 和 Sequelize 的新手,并尝试为具有引用错误的多对多关系实现错误处理。我有以下关系 Post > CategoryPost < Category 关系。我的连接表的迁移看起来像这样
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("CategoryPost", {
CategoryId: {
type: Sequelize.INTEGER,
primaryKey: true,
references: {
model: "categories",
key: "id",
},
},
PostId: { type: Sequelize.INTEGER, primaryKey: true },
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("CategoryPost");
},
};
通过引用,我尝试防止在我的连接表中发布具有不存在类别的帖子。这似乎有效。这是我的 CREATE POST 路由器,包括。 Controller 。router.post("/", async (req, res) => {
let { userUuid, title, body, categories } = req.body;
if (!categories) {
return res.status(400).json({ msg: "Pls provide category" });
}
try {
const user = await User.findOne({ where: { uuid: userUuid } });
const post = await Post.create({
userId: user.id,
title,
body,
});
await post.addCategories(categories);
return res.json(post);
} catch (err) {
console.log(err);
res.status(500).json({ error: "Something went wrong"});
}
});
它按预期工作。但是,如果我尝试插入具有不存在类别的帖子,我会遇到一个非常丑陋的“ native ”mysql 错误,我很难以良好的方式处理它。
我的目标是发送正确的错误消息“请提供有效类别的帖子”,如果有未定义的类别
我唯一能想到的就是检查 Category.find(all)。但感觉就像双重实现外键的 native 数据库功能并导致额外的请求。
对不起我的英语,我希望问题变得清楚。
最佳答案
您的 joining
表应该有自己的主键,与“发布”外键和 Category
都不相关。这是多对多表通常应该工作的一种常见方式。不幸的是,Sequelize 不支持复合主键,因此将 CategoryId
和 PostId
都指定为主键是没有用的(这肯定是第二种方式)。
而且您还忘记将 PostId
表示为 posts
的外键。
await queryInterface.createTable("CategoryPost", {
Id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
CategoryId: {
type: Sequelize.INTEGER,
references: {
model: "categories",
key: "id",
},
},
PostId: { type: Sequelize.INTEGER,
references: {
model: "posts",
key: "id",
},
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
关于mysql - Sequelize MySQL 处理查询中的 native 引用错误(ManyToMany),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66413178/