【问题标题】:foreign key is not working when I migrate迁移时外键不起作用
【发布时间】:2019-08-22 12:00:03
【问题描述】:

外键不起作用。

这是我的迁移文件

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`))

【问题讨论】:

标签: laravel laravel-5


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2019-08-29
    • 2020-02-20
    • 2021-04-21
    • 2016-04-02
    • 2017-09-04
    • 2023-03-14
    • 2012-09-08
    • 2017-06-18
    相关资源
    最近更新 更多