3

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

员工和地区

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

[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]

在此处输入图像描述

4

1 回答 1

9

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

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

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

于 2014-10-16T06:50:54.710 回答