迁移时外键不起作用

时间:2019-04-01 10:35:39

标签: laravel laravel-5

外键不起作用。

这是我的迁移文件

Schema::create('course_prospect', function (Blueprint $table) {
      $table->bigIncrements('id');

      $table->integer('prospect_id')->length(11)->unsigned();
      $table->foreign('prospect_id')->references('id')->on('prospect');

      $table->integer('course_id')->length(11)->unsigned();
      $table->foreign('course_id')->references('course_id')->on('course');

      $table->timestamps();
    });

我收到此错误

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 
1005 Can't create table `customerinquirydb`.`#sql-104c_e9` (errno: 150 
"Foreign key constraint is incorrectly formed") (SQL: alter table 
`course_prospect` add constraint `course_prospect_prospect_id_foreign` 
foreign key (`prospect_id`) references `prospect` (`id`))

1 个答案:

答案 0 :(得分:1)

在Laravel中使用外键迁移表时。该外键所在的表必须首先创建(必须存在)!因此,请确保是这种情况。