【问题标题】:Constrains in Foreign keys using database migrations使用数据库迁移的外键约束
【发布时间】:2018-01-23 16:01:25
【问题描述】:

我不知道我做错了什么,但一切看起来都很好,给了我这个错误

Foreign key constraint is incorrectly formed

关于我的迁移,但我没有发现任何问题。

迁移表一:

public function up()
    {
        Schema::create('candidate_industries', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('candidate_id')->unsigned();

            $table->foreign('candidate_id')->references('id')->on('candidates');

            $table->integer('industry_id')->unsigned();

            $table->foreign('industry_id')->references('id')->on('industries');
        });
    }

迁移编号 2:

public function up()
    {
        Schema::create('candidate_regions', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('candidate_id')->unsigned();

            $table->foreign('candidate_id')->references('id')->on('candidates');

            $table->integer('region_id')->unsigned();

            $table->foreign('region_id')->references('id')->on('regions');


        });
    }

【问题讨论】:

  • 请检查迁移文件的执行顺序。不能在它引用的表之前创建具有外键列的表。
  • 不是我的情况,我引用的表已经创建
  • 最好发布candidatesindustries 的代码,并提及可能有用的执行顺序。

标签: laravel laravel-5 laravel-5.3 database-migration


【解决方案1】:

问题是因为“候选人” id 列设置了不同的数据类型,在我的例子中是 BigInt,当我试图创建与 int 列的关系时。

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多