【问题标题】:How to implement a self-referenced relation in Sequelize?如何在 Sequelize 中实现自引用关系?
【发布时间】:2022-07-08 07:15:58
【问题描述】:

我有一个可以创建帖子的博客。这些帖子可以是多种语言:英语、西班牙语、法语。

我要创建的每张票都将被翻译。我希望每个帖子都在 db 中链接到其他语言的其他帖子。

我看到我可以在 Sequelize 上使用 self-reference,但我无法让它与这个示例一起使用。我找不到如何成功实现它。

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

    事实证明,我必须创建迁移文件才能使其完全正常工作。看来,如果您不使用 Sequelize 中的 sync() 方法,则永远不会创建这些联结表。

    来自 Sequelize 文档的示例:

    Person.belongsToMany(Person, { as: 'Children', through: 'PersonChildren' })
    

    帖子是这样的:

    • 模型文件:

    Post.belongsToMany(Post, { as: "Sibling", through: "PostSiblings" });

    • 关联表迁移文件
        module.exports = {
          async up(queryInterface, Sequelize) {
            await queryInterface.createTable("PostSiblings", {
              id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER,
              },
              postId: {
                type: Sequelize.INTEGER,
                references: {
                  model: "Posts",
                  key: "id",
                },
              },
              siblingId: {
                type: Sequelize.INTEGER,
                references: {
                  model: "Posts",
                  key: "id",
                },
              },
              createdAt: {
                allowNull: false,
                type: Sequelize.DATE,
              },
              updatedAt: {
                allowNull: false,
                type: Sequelize.DATE,
              },
            });
          },
          async down(queryInterface, Sequelize) {
            await queryInterface.dropTable("PostSiblings");
          },
        }; 
    
    • 如果你想尝试,我添加了一些种子:
        
        module.exports = {
          up: async (queryInterface, Sequelize) => {
            return queryInterface.bulkInsert(
              "PostSiblings",
              [
                {
                  postId: 1,
                  siblingId: 1,
                  createdAt: new Date(),
                  updatedAt: new Date(),
                },
                {
                  postId: 1,
                  siblingId: 2,
                  createdAt: new Date(),
                  updatedAt: new Date(),
                },
              ],
              {}
            );
            /**
             * Add seed commands here.
             *
             * Example:
             * await queryInterface.bulkInsert('People', [{
             *   name: 'John Doe',
             *   isBetaMember: false
             * }], {});
             */
          },
        
          down: async (queryInterface, Sequelize) => {
            return queryInterface.bulkDelete("PostSiblings", null, {});
            /**
             * Add commands to revert seed here.
             *
             * Example:
             * await queryInterface.bulkDelete('People', null, {});
             */
          },
        };
    

    模型中的through 属性引用了表名,因此使用了复数词。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-05
      • 2021-10-29
      • 1970-01-01
      • 2012-10-02
      • 2018-01-14
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      相关资源
      最近更新 更多