未在 MySQL、PostgreSQL 和 SQLite 中创建的 Sequelize 关联

Sequelize associations not being created in MySQL, PostgreSQL and SQLite

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

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

我已经用 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 按照文档中的说明在书籍 table 上添加 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;
    };

迁移后这两个 table 之间没有创建任何关联。 我也曾尝试在模型关联中定义自定义外键,如下所示:

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

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

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

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

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

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

      }

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