[英]Laravel 5.2 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
[英]Laravel 5.2 Cannot add foreign key constraint
我在migrations
表中注冊遷移時遇到問題,運行php artisan migrate
時出現以下錯誤:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key
constraint (SQL: alter table `surgeon_surgeon_specialty` add
constraint `surgeon_surgeon_specialty_surgeon_id_foreign` foreign key
(`surgeon_id`) references `surgeon` (`id`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
這是我目前的文件:
Surgeons Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('surgeons', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('surgeon_name', 30)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('surgeons');
}
}
Surgeon Specialties Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSpecialtiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('surgeon_specialties', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('surgeon_specialty_name');
$table->timestamps();
$table->unique(['surgeon_specialty_name','user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('surgeon_specialties');
}
}
然后我使用Laravel-5-Generators-Extended
包來生成
Surgeon Surgeon Specialty Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSurgeonSpecialtyPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('surgeon_surgeon_specialty', function (Blueprint $table) {
$table->integer('surgeon_id')->unsigned()->index();
$table->foreign('surgeon_id')->references('id')->on('surgeon')->onDelete('cascade');
$table->integer('surgeon_specialty_id')->unsigned()->index();
$table->foreign('surgeon_specialty_id')->references('id')->on('surgeon_specialties')->onDelete('cascade');
$table->primary(['surgeon_id', 'surgeon_specialty_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Schema::drop('surgeon_specialty_surgeon');
Schema::drop('surgeon_surgeon_specialty');
}
}
但是根據朋友的建議將其更改為:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSpecialtiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('surgeon_specialties', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('surgeon_specialty_name');
$table->timestamps();
$table->unique(['surgeon_specialty_name','user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('surgeon_specialties');
}
}
The table does migrate...
...但是,我仍然收到錯誤消息。 任何幫助將不勝感激。謝謝!
我在 Laravel 8 上遇到過這個問題。我不確定它是否與這篇文章相關。 當您創建的遷移發生這種情況many
的前one
部分one-to-many
的關系。 在您的情況下,我猜您在users
之前為items
創建了遷移。 只需根據您的關系重命名您的遷移文件。 您的surgeon
遷移日期應該早於在surgeon
surgeon_surgeon_specialty
表之前創建。
另外我建議聲明Surgeon
模型的表名:
class Surgeon extends Model {
public $table = 'surgeons';
}
這是因為我看到 Laravel 無法根據您的模型類確定您的表名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.