2016-11-14 70 views
0

我有一個外鍵問題。 我有誰擁有性用戶:Laravel「無法添加外鍵約束」 - 遷移

我的遷移用戶:

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('nom'); 
      $table->string('prenom'); 
      $table->string('adresse'); 
      $table->integer('cp'); 
      $table->string('ville'); 
      $table->string('email')->unique(); 
      $table->string('password'); 
      $table->rememberToken(); 
      $table->timestamps(); 
      $table->tinyInteger('admin')->nullable(); 
     }); 

    Schema::table('users', function ($table) { 
     $table->integer('sexe_id')->unsigned(); 
     $table->foreign('sexe_id')->references('id')->on('sexes'); 
    }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::drop('users'); 
}` 

我sexe遷移:

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('sexes', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('libelle'); 
      $table->timestamps(); 
     });  
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('sexes'); 
    } 

我的性別播種機:

class SexesTableSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     DB::table('sexes')->insert([ 
      [ 
       'libelle' => 'Homme', 
       'created_at' => date('Y-m-d H:i:s'), 
       'updated_at' => date('Y-m-d H:i:s'), 
      ], 
      [ 
       'libelle' => 'Femme', 
       'created_at' => date('Y-m-d H:i:s'), 
       'updated_at' => date('Y-m-d H:i:s'), 
      ] 
     ]);  
    } 
} 

我的用戶播種機:

class UsersTableSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     DB::table('users')->insert([ 
      [ 
       'name' => 'admin', 
       'nom' => 'Virlois', 
       'prenom' => 'Peter', 
       'sexe_id' => 1, 
       'email' => '[email protected]', 
       'adresse' => '12 rue Jean Rostand', 
       'cp' => 90000, 
       'ville' => 'Belfort', 
       'password' => bcrypt('admin123'), 
       'created_at' => date('Y-m-d H:i:s'), 
       'updated_at' => date('Y-m-d H:i:s'), 
       'admin' => 1, 
      ], 
      [ 
       'name' => 'test', 
       'nom' => 'Mennegain', 
       'prenom' => 'Mathieu', 
       'sexe_id' => 1, 
       'email' => '[email protected]', 
       'adresse' => '12 rue Jean Rostand', 
       'cp' => 90000, 
       'ville' => 'Belfort', 
       'password' => bcrypt('test123'), 
       'created_at' => date('Y-m-d H:i:s'), 
       'updated_at' => date('Y-m-d H:i:s'), 
       'admin' => 0, 
      ] 
     ]); 
    } 
} 

我的數據庫播種機:

class DatabaseSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     $this->call(SexesTableSeeder::class); 
     $this->call(UsersTableSeeder::class); 
     $this->call(SaisonTableSeeder::class); 
     $this->call(ProduitTypeSeeder::class); 
     $this->call(SportsTableSeeder::class); 
     $this->call(ProduitsTableSeeder::class); 
    } 
} 

當我運行:PHP的工匠遷移:刷新-seed

我有這樣的錯誤:

[Illuminate\Database\QueryException]           
    SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL 
    : alter table `users` add constraint `users_sexe_id_foreign` foreign key (` 
    sexe_id`) references `sexes` (`id`))           



    [PDOException]               
    SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 
+0

確保你的數據庫引擎是'InnoDB','MyISAM'不支持外鍵。 – shoieb0101

回答

1

遷移順序非常重要。從發佈的內容開始,首先運行用戶遷移,然後在同一遷移中嘗試創建用戶表並更改表。其他表sexes不存在

($table->foreign('sexe_id')->references('id')->on('sexes');) 

所以這就是爲什麼你會得到一個錯誤。

爲了運行用戶,性別,改變用戶或性別,用戶和改變相同的遷移,我建議分開遷移,但這不是一個好的方法來做事情,我的意思是混合遷移(創建和改變)。

+0

謝謝,它現在的作品:) –

0

大概有什麼用表和sexe_id => 1產生此錯誤。在您的UsersTableSeeder而不是硬編碼'sexe_id' => 1查詢您的sexes表的任何行,並使用該動態id