削除ボタンを押したらこのようなエラーが発生しました。
error
1SQLSTATE[23000]: Integrity constraint violation: 21451 Cannot delete or update a parent row: a foreign key constraint fails 3(`e_cook`.`ingredients`, CONSTRAINT `ingredients_recipe_id_foreign` FOREIGN KEY (`recipe_id`) REFERENCES `recipes` (`id`)) 4(SQL: delete from `recipes` where `id` = 1)
外部キー制約に問題があるのかなと思います。
recipe
1class CreateRecipesTable extends Migration 2{ 3 /** 4 * Run the migrations. 5 * 6 * @return void 7 */ 8 public function up() 9 { 10 Schema::create('recipes', function (Blueprint $table) { 11 $table->increments('id'); 12 $table->integer('user_id')->unsigned()->index(); 13 $table->string('name'); 14 $table->string('content'); 15 //$table->text('photo_url'); 16 $table->timestamps(); 17 18 // 外部キー制約 19 $table->foreign('user_id')->references('id')->on('users'); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('recipes'); 31 } 32}
ingredients
1class CreateIngredientsTable extends Migration 2{ 3 /** 4 * Run the migrations. 5 * 6 * @return void 7 */ 8 public function up() 9 { 10 Schema::create('ingredients', function (Blueprint $table) { 11 $table->increments('id'); 12 $table->integer('recipe_id')->unsigned()->index(); 13 $table->string('ingredient', 255); 14 $table->string('quantity', 255); 15 $table->timestamps(); 16 17 // 外部キー制約 18 $table->foreign('recipe_id')->references('id')->on('recipes'); 19 }); 20 } 21 22 /** 23 * Reverse the migrations. 24 * 25 * @return void 26 */ 27 public function down() 28 { 29 Schema::dropIfExists('ingredients'); 30 } 31}
howto
1class CreateHowToTable extends Migration 2{ 3 /** 4 * Run the migrations. 5 * 6 * @return void 7 */ 8 public function up() 9 { 10 Schema::create('how_tos', function (Blueprint $table) { 11 $table->increments('id'); 12 $table->integer('recipe_id')->unsigned()->index(); 13 $table->text('how_to_make'); 14 $table->timestamps(); 15 16 // 外部キー制約 17 $table->foreign('recipe_id')->references('id')->on('recipes'); 18 }); 19 } 20 21 /** 22 * Reverse the migrations. 23 * 24 * @return void 25 */ 26 public function down() 27 { 28 Schema::dropIfExists('how_tos'); 29 } 30}
どのように変更すればいいのでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。