我有两张桌子(员工和地区),
我正在尝试使用迁移创建它,不幸的是它不会让我,我得到:
[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]
答案 0 :(得分:3)
如果你有2个表互相攻击(或表中的外键反映到同一个表),你应该这样做:
你当然可以合并例如第3步和第4步,但为了便于阅读,最好将它们分开