Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
If you are getting error Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails while using php artisan migrate command after creating a migration file. You can set foreign id column value as nullable with null on delete on delete parent table record.
Create foreign key as nullable and assign null value on delete record using nullOnDelete method--PATH database\migrations\<yourmigrationfile>.php
On delete record from the user's table, it will set the column value as null for the column user_id in this table for this specific user. Like if you want to delete the user with id 1 this code snippet will set value null where the foreign key user_id = 1 in this particular table.
This code snippet with the foreign id column is helpful when you have already existing records in the child table and you want to set it as nullable.
Random Code Snippet Queries: Laravel
- JQuery each loop on json response after ajax in laravel
- Pass variable from blade to controller Laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Seed database using SQL file in Laravel
- Class 'Facade\Ignition\IgnitionServiceProvider' not found
- How to make Copy or Duplicate table row in laravel
- Count all and get 10 records after where condition in laravel
- How to set column as primary key in Laravel model
- Property [user] does not exist on this collection instance
- How to check data inserted or deleted in pivot after toggle method
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- How to access the nth object from Laravel collection object ?
- How to get records in random order in laravel
- How to create controller in laravel
- Return redirect laravel not working
- Shorter syntax for whereHas with call back function in laravel
- Print last executed query in laravel
- Permanently delete a record in laravel
- How to get last month records in Laravel
- How to get CSRF token in laravel controller
- How to pass data to route in laravel?
- Create a record if not exist in laravel
- How to pass two variables in HREF in laravel
- How to get all posts which contains comments in laravel
- How to customize pagination view in laravel