2017-03-17 13 views
0

私はLaravel 5.移行が成功するには、私のデータベースをシードしようとしているが、種子しようとしたとき、私はエラーを取得:以下 整合性制約違反Laravelでデータベースを播種時に5

[PDOException] 
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ict302`.`psas`, CONSTRAINT `psas_psa_user_ 
id_foreign` FOREIGN KEY (`psa_user_id`) REFERENCES `users` (`id`)) 

は私の移行、次のとおりです。

Userテーブル:

class CreateUsersTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('email')->unique(); 
     $table->string('password')->nullable(); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    DB::statement('SET FOREIGN_KEY_CHECKS=0;'); 
    Schema::dropIfExists('users'); 
    DB::statement('SET FOREIGN_KEY_CHECKS=1;'); 
} 

PSA表:

public function up() 
    { 
     Schema::create('psas', function (Blueprint $table) { 
      $table->increments('psa_id'); 
      $table->timestamps(); 
      $table->string('psa_email',50); 
      $table->unique('psa_email'); 
      $table->string('psa_name', 30); 
      $table->integer('psa_user_id')->unsigned()->onDelete('cascade'); 
     }); 

     Schema::table('psas', function (Blueprint $table) 
     { 
      $table->foreign('psa_user_id')->references('id')->on('users'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     DB::statement('SET FOREIGN_KEY_CHECKS=0;'); 
     Schema::dropIfExists('psas'); 
     DB::statement('SET FOREIGN_KEY_CHECKS=1;'); 
    } 

データベースシーダクラス:

public function run() 
    { 
     //Model::unguard(); 
     DB::statement('SET FOREIGN_KEY_CHECKS=0;'); 
     $this->call(AccessTableSeeder::class); 
     $this->call(HistoryTypeTableSeeder::class); 

     $this->call(UserTableSeeder::class); 
     $this->call(PsaTableSeeder::class); 
     DB::statement('SET FOREIGN_KEY_CHECKS=1;'); 
     //Model::reguard(); 
    } 

私が間違って何をやっているとどのように私はそれを修正することができますか?

+0

あなたのDBスキーマは何ですか? – GordonM

答えて

0

@Gautamナス

あなたは、コンセプトなどの下に使用して試すことができます:あなたは「PSAは」テーブルに外部キーとしてuser_idをしたいならば、内部アップ機能以下のように定義します。

$table->integer('user_id')->unsigned()->nullable(); 

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade'); 
関連する問題