2013-09-25 78 views
8

Đây là mã di cư của tôi:Laravel 4 Migration - Không thể thêm ràng buộc khoá ngoại

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'); 
} 

Khi mã để xác định ràng buộc khoá ngoại không được nhận xét ra, tôi nhận được lỗi sau trên dòng lệnh: chung lỗi: 1215 Không thể thêm ràng buộc khóa ngoài.

Bất kỳ ý tưởng nào tôi đang làm sai?

Trả lời

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

nên

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

hoặc bạn có thể sử dụng phiên bản ngắn: "Doh"

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

Trong câu nói nổi tiếng của Homer,. Cảm ơn! – StackOverflowNewbie

+5

Đừng đánh bại bản thân - Tôi chỉ có thể trả lời câu hỏi của bạn vì tôi đã làm điều tương tự. :-) – ceejayoz