1

我想創建兩個表usersroles。我的代碼如下所示:無法在Laravel 5.2中使用外鍵遷移

2014_10_12_000000_create_users_table.php

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

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->integer('role')->unsigned(); 
      $table->string('email')->unique(); 
      $table->string('password'); 
      $table->rememberToken(); 
      $table->timestamps(); 

      $table->foreign('role') 
       ->references('id') 
       ->on('roles'); 
     }); 
    } 

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

2016_03_09_004256_create_roles_table.php

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateRolesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('roles', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->timestamps(); 
     }); 
    } 

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

當我運行php artisan migrate下面的錯誤出現。

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table trucking . #sql-1240_44 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users add constraint u sers_role_foreign foreign key (role) references roles (id))

[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table trucking . #sql-1240_44 (errno: 150 "Foreign key constraint is incorrectly formed")

回答

6

你應該確保,你roles表遷移之前users表遷移運行。默認情況下,在Laravel中,您已創建了users表遷移,因此,如果只是更改了其代碼並稍後添加roles遷移,它將無法工作。您應該更改usersroles遷移文件名,以確保roles表遷移文件的開始時間戳會在users表之前。

例如,你可能有這樣的情況:

2014_10_12_000000_create_users_table.php 
2015_10_12_123552_create_roles_table.php 

,你應該重命名文件有這樣的:

2015_10_12_123652_create_users_table.php 
2015_10_12_123552_create_roles_table.php 

當然我假設你只在開發過程中使用這些遷移而且還沒有投入生產。

+0

哇它的作品!好主意!謝謝哥們。 – smartrahat

+1

@smartrahat沒問題。您需要記住,您可以只將外鍵創建到現有表中,因此有時您需要創建2個遷移(無外鍵),並在第3次遷移時在表之間添加外鍵。 –

+0

感謝您的提示。我會記住它。 – smartrahat

0
public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('firstname'); 
      $table->string('lastname'); 
      $table->text('slug'); 
      $table->string('email')->unique(); 
      $table->string('password', 60); 
      $table->decimal('fidbonus', 6, 2); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
     Schema::create('Userinfos', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('User_id')->unsigned(); 
      $table->foreign('User_id')->references('id')->on('Users'); 
      $table->string('address'); 
      $table->string('address2'); 
      $table->text('city'); 
      $table->string('zip'); 
      $table->string('country'); 
      $table->string('Phone'); 
      $table->timestamps(); 
     }); 
     Schema::create('password_resets', function (Blueprint $table) { 
      $table->string('email')->index(); 
      $table->string('token')->index(); 
      $table->timestamp('created_at'); 
     }); 
    } 
+0

我不認爲我們可以寫超過1'up()'的方法。如果需要,在相同的'up()'方法中,可以使用'Schema :: create()'兩次,一個在另一個之下。 –

+0

是的,我複製並粘貼它錯了,但多數民衆贊成,不適合編輯它,並添加和例證從我自己的項目,謝謝 –

0
public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('firstname'); 
     $table->string('lastname'); 
     $table->text('slug'); 
     $table->string('email')->unique(); 
     $table->string('password', 60); 
     $table->decimal('fidbonus', 6, 2); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 
    Schema::create('Userinfos', function (Blueprint $table) { 
     $table->increments('id'); 
     //$table->integer('User_id')->unsigned(); 
     $table->unsignedInteger('User_id'); 
     //$table->foreign('User_id')->references('id')->on('Users'); 
     $table->foreign('User_id')->references('id')->on('Users')->onDelete('cascade')->onUpdate('cascade'); 
     $table->string('address'); 
     $table->string('address2'); 
     $table->text('city'); 
     $table->string('zip'); 
     $table->string('country'); 
     $table->string('Phone'); 
     $table->timestamps(); 
    }); 
    Schema::create('password_resets', function (Blueprint $table) { 
     $table->string('email')->index(); 
     $table->string('token')->index(); 
     $table->timestamp('created_at'); 
    }); 
} 

嘗試上文code.Its做工精細