【问题标题】:Sequelize associations not being created in MySQL, PostgreSQL and SQLite未在 MySQL、PostgreSQL 和 SQLite 中创建的 Sequelize 关联
【发布时间】:2026-02-12 12:50:02
【问题描述】:

我正在使用 MYSQL 的 sequalize 定义模型中的关联。但是在迁移之后,外键没有被添加到目标模型中,如 sequelize 文档中所述。

我也尝试在模型和迁移文件中手动定义外键,但仍然没有在表之间创建关联。当我在 PhpMyAdmin 的关系视图中查看表时,没有创建外键约束或关系。

我已经在 SQLite 和 PostgreSQL 上尝试过,结果相同。我不知道我做错了什么。这是模型。

AURHOR MODEL
//One author hasMany books

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Author = sequelize.define('Author', {
    Name: DataTypes.STRING
  }, {});
  Author.associate = function(models) {
    // associations can be defined here
    Author.hasMany(models.Book)
  };
  return Author;
};

我希望 sequelize 能够按照文档中的说明在 books 表上添加 authorId,但这不会发生

 BOOK MODEL
    //Book belongs to Author
    'use strict';
    module.exports = (sequelize, DataTypes) => {
      const Book = sequelize.define('Book', {
        Title: DataTypes.STRING
      }, {});
      Book.associate = function(models) {
        // associations can be defined here
        Book.belongsTo(models.Author)
      };
      return Book;
    };

迁移后不会在这两个表之间创建关联。 我也尝试在模型关联中定义自定义外键,如下所示:

//Author model
    Author.hasMany(models.Book,{foreignKey:'AuthorId'})
 //Book model
 Book.belongsTo(models.Author,{foreignKey:'AuthorId'})

这仍然不能解决问题

我已经开始在模型中定义外键,然后在关联中引用它们,如下所示:

  'use strict';
    module.exports = (sequelize, DataTypes) => {
      const Book = sequelize.define('Book', {
        Title: DataTypes.STRING,
        AuthorId:DataTypes.INTEGER
      }, {});
      Book.associate = function(models) {
        // associations can be defined here
        Book.belongsTo(models.Author,{foreignKey:'AuthorId'})
      };
      return Book;
    };

但仍然没有创建关联

我最终决定在迁移文件中添加引用,如下所示:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Books', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      Title: {
        type: Sequelize.STRING
      },

      AuthorId:{
          type: Sequelize.INTEGER,
          references:{
          model:'Author',
          key:'id'
          }

      }

      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Books');
  }
};

但是当我运行这种迁移设置时,我得到这个错误:错误:无法创建表dbname.books(错误号:150“外键约束是我 格式不正确")

我在切换到 PostgreSQL 时遇到类似的错误。

这个问题困扰我很久了。我可能做错了什么。我正在使用 sequelize version 4.31.2 和 sequelize CLI。

【问题讨论】:

    标签: mysql postgresql express sequelize.js associations


    【解决方案1】:

    我在迁移中错误地引用了模型。 走错路了

     AuthorId:{
              type: Sequelize.INTEGER,
              references:{
              model:'Author',
              key:'id'
              }
    
          }
    

    正确的方式

      // Notes the model value is in lower case and plural just like the table name in the database
         AuthorId:{
                  type: Sequelize.INTEGER,
                  references:{
                  **model:'authors',**
                  key:'id'
                  }
    
          }
    

    这解决了我的问题。现在正在定义关联。

    【讨论】: