2017-07-27 83 views
0

我试图让migraions,但仍然有同样的错误:Laravel迁移不能添加foregin关键

[Illuminate\Database\QueryException]           
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key 
constraint (SQL 
: alter table `user_class` add constraint 
`user_class_class_id_foreign` 
foreign key (`class_id`) references `classes` (`id`))       




[PDOException]               
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key 
constraint 

我的数据库迁移是:

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

     $table->increments('id'); 
     $table->integer('user_id')->length(10)->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users'); 
     $table->integer('class_id')->length(10)->unsigned(); 
     $table->foreign('class_id')->references('id')->on('classes'); 
    }); 
} 

public function down() 
{ 

    DB::statement('SET FOREIGN_KEY_CHECKS = 0'); 
    Schema::drop('user_class'); 
    DB::statement('SET FOREIGN_KEY_CHECKS = 1'); 

} 

我认为我有这个错误因为user_classes先前在我的数据库文件中,然后类?这可能是我的问题吗?

当我做:PHP的工匠制作:回滚 - 此表:USER_CLASS是不是从我phpAdmin删除......仅此

+0

是的,父表必须在创建子表之前 – aaron0207

+0

好的,那么我应该在这一刻做什么?我怎样才能改变我的表格? – wenus

+3

只需重命名它们,它们的执行顺序与项目文件树 – aaron0207

回答

0

我觉得长度方法的问题,我检查Laravel文档,它不存在。