Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))

Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table users add constraint users_role_id_foreign foreign key (role_id) references roles (id)) error occurs when you try to migrate a file with a foreign key with $table->foreign('role_id') in laravel 8.

Answers 1
  • Create foreign key role_id in table using migration in laravel 8

    //$table->foreign('role_id')->references('id')->on('roles');
    //Replace with below code
    $table->foreignId('role_id')->references('id')->on('roles');
    
    0

    Laravel code while creating foreign id to table using migration in laravel 8.  You have to use $table->foreignId('role_id')->references('id')->on('roles'); in place of $table->foreign('role_id')->references('id')->on('roles'); while migration in laravel.

  • Back to code snippet queries related laravel