2017-02-24 98 views
1

所以我試圖在我的laravel遷移文件中設置一個外鍵,所以用戶表很簡單,但我試圖使用bigIncrements而不是站點增量。在Laravel 5.4中設置外鍵bigInteger 5.4

public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->bigIncrements('id')->unsigned(); 
     $table->string('user_id')->unique(); 
     $table->string('avatar'); 
     $table->string('name'); 
     $table->string('email')->unique(); 
     $table->string('password')->nullable(); 
     $table->rememberToken(); 
     $table->timestampsTz(); 
    }); 
} 

而當我做另一個表時,我嘗試添加外鍵給它,我得到一個錯誤,說外鍵被錯誤地形成。我很困惑,因爲我匹配列類型。這是另一張桌子。

public function up() 
{ 
    Schema::create('social_logins', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->bigIncrements('id'); 
     $table->bigInteger('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->unsigned()->index(); 
     $table->string('provider', 32); 
     $table->string('provider_id'); 
     $table->string('token')->nullable(); 
     $table->string('avatar')->nullable(); 
     $table->timestamps(); 
    }); 
} 
+0

儘量擺脫'的 - >無符號() - >指數()'末的外鍵聲明 – ntzm

回答

1

取下unsigned()

$table->bigIncrements('id')->unsigned(); 

而其他遷移應該是這樣的:

$table->bigInteger('user_id')->unsigned(); 
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
.... 
$table->index('user_id'); 
+0

非常感謝你做到了。 –

+0

這應該在文檔中更清楚,ty! – DelightedD0D

0

問題是,bigIncrements返回一個unsignedBigInteger。 爲了工作,外鍵必須是一個無符號大整數index()