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

Created at 15-Jul-2021 , By samar

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.

  • 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');
    

    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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.