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.
-
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.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
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.
Random Code Snippet Queries: Laravel
- Best Practices for Error Handling in Production Server Code (example code)
- Pagination in laravel
- Get ids in array from users table
- Laravel change date format
- Get content from web URL in laravel
- How to get tomorrow and yesterday date in laravel
- Extract only time from datetime in laravel
- Get latest record by created at in Laravel
- Use withCount() to Calculate Child Relationship Records
- How to add foreign key in laravel using migration
- Split an Eloquent Collection by half in Laravel
- Print query in laravel
- Seed database using SQL file in Laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- Insert current date time in a column using Laravel
- How to run a specific seeder class in laravel
- How to upload image in laravel 8
- How to get session in blade Laravel ?
- Credit card validation in laravel
- How to authenticate admin users in Laravel ?
- Redirect to previous page or url in laravel
- Get last week data in Laravel
- How to get selected categories on edit record with Select2
- How to check data inserted or deleted in pivot after toggle method