Sequelize errno: 150“外键约束的格式不正确”

问题描述

我在迁移具有关系的两个表时遇到问题。我想向 product.js 迁移添加外键,但它不起作用。如果我将在 await sequelize.sync(); 数据库创建良好的情况下运行应用程序。

如何解决这个问题?我对另一个迁移 useraddresses 做了同样的事情,它按我的预期工作。感谢您的帮助。

== 20210124144301-create-product:迁移 ========

错误:无法创建表 database_development.products错误号: 150 "外键约束的格式不正确")

create-category.js 迁移:

"use strict";
module.exports = {
  up: async (queryInterface,Sequelize) => {
    await queryInterface.createTable("сategories",{
      id: {
        allowNull: false,autoIncrement: true,primaryKey: true,type: Sequelize.INTEGER,},name: {
        type: Sequelize.STRING,allowNull: false,unique: true,description: Sequelize.TEXT,createdAt: {
        allowNull: false,type: Sequelize.DATE,updatedAt: {
        allowNull: false,});
  },down: async (queryInterface,Sequelize) => {
    await queryInterface.dropTable("сategories");
  },};

create-product.js 迁移:

"use strict";
module.exports = {
  up: async (queryInterface,Sequelize) => {
    await queryInterface.createTable("products",categoryId: {
        type: Sequelize.INTEGER,references: {
          model: "categories",key: "id",description: {
        type: Sequelize.TEXT,price: {
        type: Sequelize.DOUBLE(11,2).UNSIGNED,defaultValue: 0,Sequelize) => {
    await queryInterface.dropTable("products");
  },};

category.js 模型:

"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize,DataTypes) => {
  class Category extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      this.hasMany(models.Product,{
        foreignKey: "categoryId",});
    }
  }
  Category.init(
    {
      name: {
        type: DataTypes.STRING,description: DataTypes.TEXT,{
      sequelize,tableName: "categories",modelName: "Category",}
  );
  return Category;
};

product.js 模型:

"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize,DataTypes) => {
  class Product extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      this.belongsTo(models.Category,});
    }
  }
  Product.init(
    {
      name: {
        type: DataTypes.STRING,description: {
        type: DataTypes.TEXT,price: {
        type: DataTypes.DOUBLE(11,tableName: "products",modelName: "Product",}
  );
  return Product;
};

解决方法

您需要在产品和类别模型文件中添加主键 (id) 也会更改您的模型关联。

product.js

 "use strict";
const { Model } = require("sequelize");
module.exports = (sequelize,DataTypes) => {
  class Product extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Product.belongsTo(models.Category,{
        foreignKey: "categoryId",});
    }
  }
  Product.init(
    {
      productId: {
        allowNull: false,autoIncrement: true,primaryKey: true,type: DataTypes.INTEGER
      },name: {
        type: DataTypes.STRING,allowNull: false,},description: {
        type: DataTypes.TEXT,price: {
        type: DataTypes.DOUBLE(11,2).UNSIGNED,defaultValue: 0,categoryId: {
        type: DataTypes.INTEGER
      }
    },{
      sequelize,tableName: "products",modelName: "Product",}
  );
  return Product;
};

类别.js

"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize,DataTypes) => {
  class Category extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Category.hasMany(models.Product,});
    }
  }
  Category.init(
    {
      categoryId: {
        allowNull: false,unique: true,description: DataTypes.TEXT,tableName: "categories",modelName: "Category",}
  );
  return Category;
};