【问题标题】:Laravel Migration Two table foreign key dependenciesLaravel 迁移两表外键依赖
【发布时间】:2014-10-16 06:23:08
【问题描述】:

我有两张表(员工和地区),

我正在尝试使用迁移来创建它,不幸的是它不会让我,我得到:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table regions` add constraint regions_create_by_foreign foreign key (`create_by`) references `employees` (`id`))

[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

以下是我的迁移代码:

FILE: 2014_10_11_052414_create_regions_table.php  //this is the migration file for regions

    Schema::create('regions', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name',50)->unique();
        $table->integer('head_office_flag');
        $table->integer('create_by')->unsigned();
        $table->foreign('create_by')->references('id')->on('employees'); //this is the problem
        $table->datetime('create_datetime')->default(DB::raw('CURRENT_TIMESTAMP'));
    });

FILE: 2014_10_12_110447_create_employees_table.php //this is the migration file for employees

    Schema::create('employees', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('first_name',50);
        $table->string('surname',50);
        $table->string('email',100);
        $table->integer('region_id');   
        $table->string('photos',255)->nullable();
        $table->string('mobile_no',50);
        $table->string('office_no',50)->nullable();
        $table->string('landline_no',50)->nullable();
        $table->integer('create_by')->unsigned();                    
        $table->foreign('create_by')->references('id')->on('employees'); //this is the problem to reference to itself
        $table->datetime('create_datetime')->default(DB::raw('CURRENT_TIMESTAMP'));
        $table->integer('status');
    });

我的问题是:

  • 我需要更改迁移顺序吗?考虑到两者都有外键。 *注意,我尝试通过重命名文件并在文件名中使用较早的时间戳来更改顺序

  • 我应该添加哪些额外的代码?

  • 我必须在我的播种机中添加额外的代码吗?

提前致谢!干杯。

更新

@Marcin:我已将其更新为:

FILE: 2014_10_11_052414_create_regions_table.php

    Schema::create('regions', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name',50)->unique();
        $table->integer('head_office_flag');
        $table->integer('create_by')->unsigned();
        $table->datetime('create_datetime')->default(DB::raw('CURRENT_TIMESTAMP'));
    });

FILE: 2014_10_11_110447_create_employees_table.php

    Schema::create('employees', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('first_name',50);
        $table->string('surname',50);
        $table->string('email',100);
        $table->integer('region_id')->unsigned();
        $table->string('photos',255)->nullable();
        $table->string('mobile_no',50);
        $table->string('office_no',50)->nullable();
        $table->string('landline_no',50)->nullable();
        $table->integer('create_by')->unsigned();
        $table->datetime('create_datetime')->default(DB::raw('CURRENT_TIMESTAMP'));
        $table->integer('status');
    });

FILE: 2014_10_12_065402_alter_regions_table_FK.php

    Schema::table('regions', function($table)
    {
        $table->foreign('create_by')->references('id')->on('employees');
    });

FILE: 2014_10_12_070219_alter_employees_table_FK.php

    Schema::table('employees', function($table)
    {
        $table->foreign('region_id')->references('id')->on('regions');
        $table->foreign('create_by')->references('id')->on('employees');
    });

我仍然收到错误:

![错误][2]

【问题讨论】:

    标签: php laravel artisan-migrate


    【解决方案1】:

    如果您有 2 个相互引用的表(或表中的外键引用同一个表),您应该这样做:

    1. 为第一个表创建迁移 - 此处不要使用任何外键
    2. 为第二个表创建迁移 - 此处不要使用任何外键
    3. 为第一个表创建迁移 - 在这里您只添加外键
    4. 为第二个表创建迁移 - 在这里您只添加外键

    您当然可以合并例如第 3 步和第 4 步,但可能最好将它们分开以提高可读性

    【讨论】:

    • 我已经试过你的回答了,还是报错,我已经更新了我的问题,请看一下。谢谢再次更新
    • @Ponce 迁移可能存在一些问题。您应该删除 migrations 表和 employeesregions 并再次运行迁移
    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2016-06-06
    • 2019-09-23
    • 2017-06-05
    • 2019-01-31
    • 2014-08-09
    • 2014-09-27
    相关资源
    最近更新 更多