2016-06-12 134 views
0

我使用Laravel 5.2,有一個錯誤,當我運行php artisan migrate,如下所示:Laravel 5.2:`PHP工匠migrate`的錯誤

2016_06_12_134655_create_categories_table.php

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

    $table->increments('id'); 
    $table->string('category'); 
    $table->timestamps(); 

    }); 
} 

2016_06_12_134659_create_goods_table .php

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

    $table->increments('id'); 
    $table->string('name'); 
    $table->string('unit_price'); 
    $table->integer('user_id')->unsigned(); 
    $table->tinyInteger('category_id')->unsigned(); 
    $table->foreign('user_id')->references('id')->on('users'); 
    $table->foreign('category_id')->references('id')->on('categories'); 
    $table->timestamps(); 

    }); 
} 

$ PHP工匠遷移

enter image description here

我該怎麼辦?

+0

看來你'category_id'因爲沒有參考。你的分類表應該是第一個。 –

回答

1

它可能會抱怨你在category_id上使用tinyInteger,試着將它設置爲integer - 假設你的分類表存在。如果沒有,則需要確保任何具有外鍵約束的遷移都將它們的相關表遷移到它們之前。沒有看到你的分類表,我可能想知道你的id的數據類型是否也一樣。

0

被引用的表categories有一個int類型的主鍵。保持相同的引用列category_id如果你要保持外鍵約束,在行定義的:

$table->foreign('category_id')->references('id')->on('categories'); 
0

當您使用主鍵$table->Increments('id');

你應該使用Integer爲外鍵

$table-> unsignedInteger('fk_id'); 
    $table->foreign('fk_id')->references('id')->on('table_name'); 

當您使用主鍵$table->tinyIncrements('id');

當您使用主鍵$table->smallIncrements('id');

你應該使用unsignedSmallInteger作爲一個外鍵

$table-> unsignedSmallInteger('fk_id'); 
    $table->foreign('fk_id')->references('id')->on('table_name'); 

你應該使用unsignedTinyInteger作爲一個外鍵

$table-> unsignedTinyInteger('fk_id'); 
    $table->foreign('fk_id')->references('id')->on('table_name'); 

當你使用主鍵$table->mediumIncrements('id');

你應該使用unsignedMediumInteger作爲一個外鍵

$table-> unsignedMediumInteger('fk_id'); 
    $table->foreign('fk_id')->references('id')->on('table_name');