2 つのテーブル (従業員と地域) があります。
移行を使用して作成しようとしていますが、残念ながらできません。取得しています:
[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]