mysql - Sequelize MySQL 处理查询中的 native 引用错误(ManyToMany)

标签 mysql error-handling sequelize.js many-to-many

我是 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 错误,我很难以良好的方式处理它。
enter image description here
我的目标是发送正确的错误消息“请提供有效类别的帖子”,如果有未定义的类别
我唯一能想到的就是检查 Category.find(all)。但感觉就像双重实现外键的 native 数据库功能并导致额外的请求。
对不起我的英语,我希望问题变得清楚。

最佳答案

您的 joining 表应该有自己的主键,与“发布”外键和 Category 都不相关。这是多对多表通常应该工作的一种常见方式。不幸的是,Sequelize 不支持复合主键,因此将 CategoryIdPostId 都指定为主键是没有用的(这肯定是第二种方式)。
而且您还忘记将 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/

相关文章:

mysql - 在单个资源中连接到多个 mysql 数据库

error-handling - 404 劫持

javascript - 当 promise 失败时如何捕捉?

php - 将失败的登录尝试写到Apache error_log

过去 24 小时内最受欢迎的 PHP MySQL 查询

java - 使用具有多个可能的 "where"语句的 java 查询 mysql

javascript - 从具有另一个异步函数的异步函数返回数据

mysql - 使用事务将巨大的 CSV 文件导入 MySQL

mysql - 如果该行已存在,则更新而不是插入

sequelize.js - 在 sequelize 中一次同步多个表