以下のように3つのマイグレーションファイルに外部キー制約を設定したところ、2019_05_23_224158_create_events_tableのマイグレートがうまくいきませんでした。
エラーメッセージは、
SQLSTATE[HY000]: General error: 1005 Can’t create table ‘hel_mane.#sql-12ca_e’ (errno: 150) (SQL: alter table icons
add constraint icons_event_id_foreign
foreign key (event_id
) references events
(id
) on delete cascade)
と出ています。
マイグレートを行う前に、各テーブルに以下の操作をしました。
①idの型をbigIncrementsからincrementsに変更
②user_idやevent_idにunsigned()を付記
③users_tableのfunction downに
Schema::dropIfExists(‘memos’);
Schema::dropIfExists(‘icons’);
を追加しました。
①②はusers_tableのidと外部キーのidの型を合わせる目的で設定しています。
上記のエラーを解消するための方法を教えてください。
2014_10_12_000000_create_users_table
php
1<?php 2 use Illuminate\Support\Facades\Schema; 3use Illuminate\Database\Schema\Blueprint; 4use Illuminate\Database\Migrations\Migration; 5 class CreateUsersTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::create('users', function (Blueprint $table) { 15 $table->increments('id'); 16 $table->string('name'); 17 $table->string('email')->unique(); 18 $table->timestamp('email_verified_at')->nullable(); 19 $table->string('password'); 20 $table->rememberToken(); 21 $table->timestamps(); 22 }); 23 } 24 /** 25 * Reverse the migrations. 26 * 27 * @return void 28 */ 29 public function down() 30 { 31 Schema::dropIfExists('memos'); 32 Schema::dropIfExists('icons'); 33 Schema::dropIfExists('users'); 34 } 35}
2019_05_23_224049_create_memos_table
php
1<?php 2 use Illuminate\Support\Facades\Schema; 3use Illuminate\Database\Schema\Blueprint; 4use Illuminate\Database\Migrations\Migration; 5 class CreateMemosTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::create('memos', function (Blueprint $table) { 15 $table->increments('id'); 16 $table->datetime('memo_day'); 17 $table->text('memo'); 18 $table->integer('user_id')->unsigned(); 19 $table->timestamps(); 20 $table 21 ->foreign('user_id') 22 ->references('id') 23 ->on('users') 24 ->onDelete('cascade'); 25 }); 26 } 27 /** 28 * Reverse the migrations. 29 * 30 * @return void 31 */ 32 public function down() 33 { 34 Schema::dropIfExists('memos'); 35 } 36}
2019_05_23_224137_create_icons_table
php
1<?php 2 use Illuminate\Support\Facades\Schema; 3use Illuminate\Database\Schema\Blueprint; 4use Illuminate\Database\Migrations\Migration; 5 class CreateIconsTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::create('icons', function (Blueprint $table) { 15 $table->increments('id'); 16 $table->datetime('event_day'); 17 $table->text('description'); 18 $table->integer('user_id')->unsigned(); 19 $table->integer('event_id')->unsigned(); 20 $table->timestamps(); 21 $table 22 ->foreign('user_id') 23 ->references('id') 24 ->on('users') 25 ->onDelete('cascade'); 26 $table 27 ->foreign('event_id') 28 ->references('id') 29 ->on('events') 30 ->onDelete('cascade'); 31 }); 32 } 33 /** 34 * Reverse the migrations. 35 * 36 * @return void 37 */ 38 public function down() 39 { 40 Schema::dropIfExists('icons'); 41 } 42}
2019_05_23_224158_create_events_table.php
php
1<?php 2 use Illuminate\Support\Facades\Schema; 3use Illuminate\Database\Schema\Blueprint; 4use Illuminate\Database\Migrations\Migration; 5 class CreateEventsTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::create('events', function (Blueprint $table) { 15 $table->increments('id'); 16 $table->text('name'); 17 $table->path('text'); 18 $table->timestamps(); 19 }); 20 } 21 /** 22 * Reverse the migrations. 23 * 24 * @return void 25 */ 26 public function down() 27 { 28 Schema::dropIfExists('events'); 29 } 30}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/29 02:22