0

Foreign key is not working.

This is my migration file

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

I'm getting this error

 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`))
Jazim Max
  • 111
  • 1
  • 10
  • 4
    Possible duplicate of [laravel errno 150 foreign key constraint is incorrectly formed](https://stackoverflow.com/questions/40863517/laravel-errno-150-foreign-key-constraint-is-incorrectly-formed) – CapaNif Apr 01 '19 at 10:38
  • i think here in this line `id` should be replaced by `prospect_id` `$table->foreign('prospect_id')->references('id')->on('prospect');` – Ash-b Apr 01 '19 at 10:40
  • Can you add your prospect and course table too ? – Md.Sukel Ali Apr 01 '19 at 10:43

1 Answers1

2

When migrating a table with a foreign key in Laravel. The table where that foreign key is, MUST be created first (must exist)! So please make sure that is the case.

lewis4u
  • 14,256
  • 18
  • 107
  • 148