2013-09-25 60 views
8

這裏是我的移民代碼:Laravel 4遷移 - 不能添加外鍵約束

public function up() 
{ 
    Schema::create('foos', function(Blueprint $table) { 
     // Primary key 
     $table->increments('id'); 

     // Standard 
     $table->engine = 'InnoDB'; 
     $table->timestamps(); 
     $table->softDeletes(); 
    }); 

    Schema::create('bars', function(Blueprint $table) { 
     // Primary key 
     $table->increments('id'); 

     // Define foreign key 
     $table->integer('foo_id')->unsigned; 

     // Foreign key contraints 
     // NOTE: causes "General error: 1215 Cannot add foreign key constraint" 
     // $table->foreign('foo_id')->references('id')->on('foos'); 

     // Standard 
     $table->engine = 'InnoDB'; 
     $table->timestamps(); 
     $table->softDeletes(); 
    }); 
} 

public function down() 
{ 
    Schema::drop('foos'); 
    Schema::drop('bars'); 
} 

當沒有被註釋掉定義外鍵約束的代碼,我得到的命令行上出現以下錯誤:一般錯誤:1215無法添加外鍵約束

任何想法我做錯了什麼?

回答

16
$table->integer('foo_id')->unsigned; 

應該

$table->integer('foo_id')->unsigned(); 

,或者您可以使用短版本: 「衛生署」

$table->unsignedInteger('foo_id'); 
+1

在荷馬的名言。謝謝! – StackOverflowNewbie

+5

不要打垮自己 - 我只能回答你的問題,因爲我做了完全相同的事情。 :-) – ceejayoz