rfp*_*pdl 3 php laravel artisan-migrate
我有两张桌子(员工和地区),

我正在尝试使用迁移来创建它,不幸的是它不会让我,我得到:
[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
Run Code Online (Sandbox Code Playgroud)
以下是我的迁移代码:
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');
});
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我需要改变迁移顺序吗?考虑到两者都有彼此的外键。*注意,我尝试通过重命名文件并在文件名中使用较早的时间戳来更改序列
我应该添加什么额外的代码?
我必须在我的播种机中添加额外的代码吗?
提前致谢!干杯。
更新
@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');
});
Run Code Online (Sandbox Code Playgroud)
我仍然收到错误:
![错误][2]

如果您有 2 个相互引用的表(或表中引用同一个表的外键),您应该执行以下操作:
您当然可以合并例如第 3 步和第 4 步,但为了可读性,最好将它们分开
| 归档时间: |
|
| 查看次数: |
3923 次 |
| 最近记录: |