【问题标题】:Sequelize errno: 150 "Foreign key constraint is incorrectly formed"Sequelize errno: 150 "外键约束格式不正确"
【发布时间】:2021-04-28 12:25:20
【问题描述】:

我在迁移两个具有关系的表时遇到问题。我想为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,
        type: Sequelize.DATE,
      },
    });
  },
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable("сategories");
  },
};

create-product.js迁移:

"use strict";
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("products", {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      categoryId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: "categories",
          key: "id",
        },
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: false,
      },
      price: {
        type: Sequelize.DOUBLE(11, 2).UNSIGNED,
        defaultValue: 0,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    });
  },
  down: async (queryInterface, 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,
        allowNull: false,
        unique: true,
      },
      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, {
        foreignKey: "categoryId",
      });
    }
  }
  Product.init(
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      description: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      price: {
        type: DataTypes.DOUBLE(11, 2).UNSIGNED,
        defaultValue: 0,
      },
    },
    {
      sequelize,
      tableName: "products",
      modelName: "Product",
    }
  );
  return Product;
};

【问题讨论】:

    标签: node.js sequelize.js sequelize-cli


    【解决方案1】:

    您需要在产品和类别模型文件中添加主键(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,
            allowNull: false,
          },
          price: {
            type: DataTypes.DOUBLE(11, 2).UNSIGNED,
            defaultValue: 0,
          },
          categoryId: {
            type: DataTypes.INTEGER
          }
        },
        {
          sequelize,
          tableName: "products",
          modelName: "Product",
        }
      );
      return Product;
    };
    

    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) {
          Category.hasMany(models.Product, {
            foreignKey: "categoryId",
          });
        }
      }
      Category.init(
        {
          categoryId: {
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            type: DataTypes.INTEGER
          },
          name: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true,
          },
          description: DataTypes.TEXT,
        },
        {
          sequelize,
          tableName: "categories",
          modelName: "Category",
        }
      );
      return Category;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 2018-03-02
      • 2020-07-07
      • 1970-01-01
      • 2017-04-13
      • 2018-09-04
      • 2019-09-17
      • 2020-12-16
      相关资源
      最近更新 更多