I am trying to make a migration in my Laravel project, but i get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'department_id' doesn't exist in table (SQL: alter table courses
add index courses_department_id_index
(department_id
))
I removed 1 line from my table.php file which i thought caused the error, and then it worked just fine. Could someone explain where my syntax is wrong in the following line?
$table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
CodePudding user response:
The error means that there is no department_id
column that references the foreign key, Because you should create column before creating a foreign key, I think your migration file should contains the following:
$table->unsignedBigInteger('department_id');
$table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');