2016-03-28 84 views
1

我看到噸StackOverflow上的問題,但由於某些原因的答案並不適用於以下內容:錯誤1215;繼續得到本外鍵錯誤關於我的錯誤

我有三個表如下:

public function up() 
{ 
    Schema::create('activities_nl', function(Blueprint $table){ 

     $table->increments('id'); 
     $table->integer('activity_id')->unique(); 
     $table->string('title'); 
     $table->string('location'); 
     $table->string('price'); 
     $table->string('age_indication')->nullable(); 
     $table->longText('registration')->nullable(); 
     $table->longText('mandatory')->nullable(); 
     $table->longText('description')->nullable(); 
     $table->string('special_title')->nullable(); 
     $table->string('academy')->nullable(); 
     $table->timestamps(); 

     $table->integer('en_table')->nullable(); 
     $table->integer('de_table')->nullable(); 
    }); 
} 

public function up() 
{ 
    Schema::create('activities_de', function(Blueprint $table){ 

     $table->increments('id'); 
     $table->integer('activity_id')->unique(); 
     $table->string('title'); 
     $table->string('location'); 
     $table->string('age_indication')->nullable(); 
     $table->string('price'); 
     $table->longText('registration')->nullable(); 
     $table->longText('mandatory')->nullable(); 
     $table->longText('description')->nullable(); 
     $table->string('special_title')->nullable(); 
     $table->string('academy')->nullable(); 
     $table->timestamps(); 

     $table->integer('nl_table')->nullable(); 
     $table->integer('en_table')->nullable(); 
    }); 
} 

public function up() 
{ 
    Schema::create('activities_en', function(Blueprint $table){ 

     $table->increments('id'); 
     $table->integer('activity_id')->unique(); 
     $table->string('title'); 
     $table->string('location'); 
     $table->string('age_indication')->nullable(); 
     $table->string('price'); 
     $table->longText('registration')->nullable(); 
     $table->longText('mandatory')->nullable(); 
     $table->longText('description')->nullable(); 
     $table->string('special_title')->nullable(); 
     $table->string('academy')->nullable(); 
     $table->timestamps(); 

     $table->integer('nl_table')->nullable(); 
     $table->integer('de_table')->nullable(); 
    }); 
} 

我試圖通過increments('id')而不是integer('activity_id)來連接它們。下面是連接外鍵的遷移。

public function up() 
{ 
    Schema::table('activities_nl', function(Blueprint $table){ 

     $table->foreign('en_table')->references('activity_id')->on('activities_en'); 
     $table->foreign('de_table')->references('activity_id')->on('activities_de'); 
    }); 

    Schema::table('activities_de', function(Blueprint $table){ 

     $table->foreign('en_table')->references('activity_id')->on('activities_en'); 
     $table->foreign('nl_table')->references('activity_id')->on('activities_nl'); 
    }); 

    Schema::table('activities_en', function(Blueprint $table){ 

     $table->foreign('nl_table')->references('activity_id')->on('activities_nl'); 
     $table->foreign('de_table')->references('activity_id')->on('activities_de'); 
    }); 
} 

由於某種原因,我不斷收到一個外鍵約束..是不是因爲laravel不喜歡我試圖引用不同的ID?如果是的話,是有這一種變通方法..

還是純粹,我俯瞰拼錯?

編輯:從unsignedInteger改爲只爲整數,應該是國外的所有列。 編輯:添加 - >可空()來activity_id所以這將是與其他列

編輯如出一轍:由於@shadow我想通了,沒有對ACTIVITY_ID沒有索引..我所要做的就是化妝它獨一無二。

+0

什麼是你的問題在這裏... –

+0

我不斷收到錯誤1215:不能添加外鍵約束 – Hoffie

+0

可能的原因似乎activities_en表ACTIVITY_ID在activities_nl表中是可以爲空但不能爲空的...對於外鍵列數據類型長度和所有屬性應該是相同的。 –

回答

1

的問題是,ACTIVITY_ID被定義爲整數,whike持外鍵被定義爲無符號整數的字段。這是一種類型不匹配。所有字段應該定義爲整數或無符號整數。

查看foreign keys MySQL文檔:

Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

此外,你需要創建兩個引用和索引引用的字段爲外鍵進行設置。 MySQL只,如果不存在創建在引用字段上的索引:

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.

+0

三江源,我所需要的要做的就是添加 - > unique()而不是 - > nullable()。 – Hoffie