【问题标题】:Sequelize migration error 150 in one to many relationshipSequelize 一对多关系中的迁移错误 150
【发布时间】:2021-07-16 01:16:50
【问题描述】:

我将正在开发的 Web 应用程序从 Windows 克隆到 Ubuntu 机器中。这个应用程序具有模块化结构,包含一个 Node JS 服务器、一个 MYSQL DB 和一个带有 React 的客户端。

我正在为数据库控制器使用 Sequelize CLI。

当我在 Ubuntu 中创建数据库并运行迁移时,我得到了

错误:无法创建表icsi_app_development.phases(错误号:150 "外键约束格式不正确")

这在具有相同依赖关系的 Windows 中运行良好:

Sequelize CLI [节点:14.17.2,CLI:6.2.0,ORM:6.6.2]

对此关联错误有任何想法吗?非常感谢!

这是我的 create-phase.js

'use strict';
module.exports = {
  up: async (queryInterface, DataTypes) => {
    await queryInterface.createTable('phases', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER
      },
      uuid: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4
      },
      projectId: {
        type: DataTypes.INTEGER,
        references: {model: "Projects", key: "id"},
        onDelete: "CASCADE"
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
      },
      description: {
        type: DataTypes.STRING,
        allowNull: false
      },
      isActive: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      createdAt: {
        allowNull: false,
        type: DataTypes.DATE
      },
      updatedAt: {
        allowNull: false,
        type: DataTypes.DATE
      }
    });
  },
  down: async (queryInterface, DataTypes) => {
    await queryInterface.dropTable('phases');
  }
};

这是我的 create-project.js

'use strict';
module.exports = {
  up: async (queryInterface, DataTypes) => {
    await queryInterface.createTable('projects', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER
      },
      uuid: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4
      },
      nume: {
        type: DataTypes.STRING,
        allowNull: false
      },
      cod_identificare: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
      },
      departament: {
        type: DataTypes.STRING,
        allowNull: false
      },
      createdAt: {
        allowNull: false,
        type: DataTypes.DATE
      },
      updatedAt: {
        allowNull: false,
        type: DataTypes.DATE
      }
    });
  },
  down: async (queryInterface, DataTypes) => {
    await queryInterface.dropTable('projects');
  }
};

这是我的项目模型

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Project 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) {
      // define association here
    }
    // toJSON(){
    //   return {...this.get(), id: undefined}
    // }
  };
  Project.init({
    nume: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: "Proiectul trebuie sa aiba un nume"},
        notEmpty: {msg: "Proiectul trebuie sa aiba un nume"}
      }
    },
    cod_identificare: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        notNull: {msg: "Proiectul trebuie sa prezinte un cod de identificare unic"},
        notEmpty: {msg: "Proiectul trebuie sa prezinte un cod de identificare unic"}
      }
    },
    departament: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: "Proiectul trebuie sa fie atribuit unui departament"},
        notEmpty: {msg: "Proiectul trebuie sa fie atribuit unui departament"}
      }
    },
    uuid: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4
    }
  }, 
  {
    sequelize,
    tableName: "projects",
    modelName: 'Project',
  });

  Project.associate = (models) => {
    Project.hasMany(models.Phase, {
      foreignKey: 'projectId',
      as: "phases"
    });

    Project.belongsToMany(models.User, {
      foreignKey: 'projectId',
      through: 'users_projects',
      as: "users"
    });
  }

  return Project;
};

这是我的阶段模型

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Phase 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) {
      // define association here
    }
  };
  Phase.init({
    projectId: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      validate: {
        notEmpty: {msg: "Faza proiectului trebuie sa prezinte un titlu"},
        notNull: {msg: "Faza proiectului trebuie sa prezinte un titlu"}
      }
    },
    description: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: {msg: "Faza proiectului trebuie sa prezinte o descriere"},
        notNull: {msg: "Faza proiectului trebuie sa prezinte o descriere"}
      }
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    uuid: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4
    }
  }, {
    sequelize,
    tableName: "phases",
    modelName: 'Phase',
  });

  Phase.associate = (models) => {
    Phase.belongsTo(models.Project, {
      foreignKey: 'projectId',
      as: "projects"
    });

    Phase.hasMany(models.File, {
      foreignKey: "phaseId",
      as: "files"
    });
  }

  return Phase;
};

【问题讨论】:

  • 共享完整运行项目,或共享所有模型

标签: javascript mysql node.js sequelize.js


【解决方案1】:

我认为有两件事可能会导致错误。

  1. 如果您正在运行迁移以在 Project 表之前创建 Phases 表,我相信您会遇到这个问题。在软件引用 Projects 表及其 id 列之前,您将创建一个 projectId 外键。

  2. 大写在 SQL 中通常不是问题,但 Sequelize 可能会遇到 Projects 迁移的 createTable 方法中的“projects”和 projectId.references 的模型属性中的“Projects”的不同大小写的问题。阶段迁移。

【讨论】:

  • 谢谢!那是我的问题,在创建父表之前运行迁移。
猜你喜欢
  • 2018-10-21
  • 1970-01-01
  • 2015-02-15
  • 2019-11-20
  • 2018-06-14
  • 2016-03-23
  • 2021-12-27
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多