2016-01-20 111 views
0

我有这四个表,但我在Laravel雄辩中得到了上述错误。任何人都可以告诉我在这里错过了什么?但是,当我删除外部方法迁移工作正常。获取错误:SQLSTATE [HY000]:一般错误:1215无法在Laravel 5中添加外键约束

Schema::create('students', function (Blueprint $table) { 
     $table->increments('id')->unsigned(); 
     $table->string('first_name'); 
     $table->string('last_name'); 
     $table->string('middle_name'); 
     $table->date('birthdate'); 
     $table->integer('degree_id')->unsigned(); 
     $table->index(['id','first_name', 'last_name']); 
     $table->timestamps(); 

     $table->foreign('degree_id') 
       ->references('id') 
       ->on('degrees'); 


    }); 

我包括无符号方法,但仍然得到上述错误。

Schema::create('degrees', function (Blueprint $table) { 
     $table->increments('id')->unsigned(); 
     $table->string('description'); 
     $table->string('category_id'); 
     $table->integer('duration'); 
     $table->string('sub_code'); 
     $table->timestamps(); 

     $table->foreign('sub_code') 
       ->references('id') 
       ->on('courses'); 
    }); 

下面是另外两张表。

 Schema::create('instructors', function (Blueprint $table) { 
     $table->increments('id')->unsigned(); 
     $table->string('first_name'); 
     $table->string('middle_name'); 
     $table->string('last_name'); 
     $table->date('birthdate'); 
     $table->index(['id', 'first_name', 'last_name']); 
     $table->timestamps(); 
    }); 

而最后的表:

Schema::create('courses', function (Blueprint $table) { 
     $table->string('id')->unique(); 
     $table->string('description'); 
     $table->integer('no_of_units'); 
     $table->string('room_id'); 
     $table->date('schedule');   
     $table->integer('instructor_id')->unsigned(); 
     $table->timestamps(); 

     $table->foreign('instructor_id') 
       ->references('id') 
       ->on('instructors'); 

    }); 
+0

也看看这里:http://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint-in-laravel –

回答

1

的迁移文件的顺序很重要。如果您在degrees表之前首先创建students表,则外键约束将失败,因为degrees表尚未创建。

要解决此问题,您可以将所有外键约束移动到应该最后运行的迁移create_foreign_key_constraints_migration

+0

谢谢我会尝试 –

+0

@CodeProject有freate_foreign_key错误表仍然是相同的。 –

相关问题