2016-09-03 23 views
1
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table `users`) 

これは数回答えられたことはわかっていますが、私の移行では何が間違っているのかわかりません。Laravel Integrityの制約がデータベース移行時に発生する

まず私は、私は、通知の移行呼び出す

public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->integer('partner_id')->unsigned(); 
     $table->string('email', 70)->unique(); 
     $table->string('first_name', 50); 
     $table->string('last_name', 50); 
     $table->string('password', 60); 
     $table->string('image', 200)->nullable(); 
     $table->string('gender', 10)->nullable(); 
     $table->string('phone', 25)->nullable(); 
     $table->string('nationality', 50)->nullable(); 
     $table->string('address', 200)->nullable(); 
     $table->boolean('is_active')->default(0); 
     $table->string('single_signon', 30)->nullable(); 

     // Checks if mysql or mariadb supports json data type 
     if ((DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')) { 
      $table->json('settings')->nullable(); 
     } else { 
      $table->text('settings')->nullable(); 
     } 

     $table->softDeletes(); 
     $table->rememberToken(); 
     $table->timestamps(); 
     $table->foreign('partner_id')->references('id')->on('partners')->onDelete('cascade'); 
    }); 
} 

public function down() 
{ 
    Schema::table('users', function(Blueprint $table) { 
     $table->dropForeign('users_partner_id_foreign'); 
    }); 

    Schema::drop('users'); 
} 

最終ユーザーの移行を呼び出す

public function up() 
{ 
    Schema::create('partners', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->integer('admin_id')->unsigned(); 
     $table->string('company_name', 50); 
     ... 
} 
public function down() 
{ 
    Schema::drop('partners'); 
} 

パートナーの移行を呼び出す:

public function up() 
    { 
     Schema::create('notifications', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 
      $table->increments('id'); 
      $table->integer('user_id')->unsigned(); 
      $table->string('notification_type', 10); 

      // Checks if mysql or mariadb supports json data type 
      if ((DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')) { 
       $table->json('notification')->nullable(); 
      } else { 
       $table->text('notification')->nullable(); 
      } 
      $table->boolean('seen'); 
      $table->timestamps(); 
      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
     }); 
    } 
public function down() 
{ 
    Schema::table('notifications', function(Blueprint $table) { 
     $table->dropForeign('notifications_user_id_foreign'); 
    }); 
    Schema::drop('notifications'); 
} 

これで何が間違っているのか分かりますか?私はlaravel 5.3とphp7を使用しています

+0

ユーザーの移行でup()のコード全体を表示 –

+0

コードを更新しました。ありがとうございました –

+0

ロールバックを呼び出そうとしていますか? –

答えて

0

問題は移行順序です。 Laravelはusersテーブルの参照を持っているuser_id列を含めnotificationsテーブル、前usersテーブルを削除しようとするためです

public function down(){ 
    Schema::drop('notifications'); 
    Schema::drop('users'); 
    Schema::drop('partners'); 
} 

を:それは1つのファイルに移行を含め、このように、親の前に子供をドロップする方が良いでしょう。

関連する問題