I have undegraded my project from Laravel 7 to 8. but the problem is when i run the seeding command php artisan db:seed it shows an error SQLSTATE[23000]: Integrity constraint violation:. it's because of it seed the previous seeding data which I run before. I have also done before run seeding command
Add Database\Seeders namespace at top of DatabaseSeeder.php and other Seeder files
Replace folder name seeds to seeders located at \database\ folder
Update composer.json like below:
"autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" }
},
Finally, run below commands
composer dump-autoload
php artisan db:seed
I have also change the DatabaseSeeder command $this->call(DeliveryAddressTableSeeder::class);
DeliveryAddressTableSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\DeliveryAddress;
class DeliveryAddressTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$deliveryRecords = [
['id' => 1, 'user_id' => 1, 'name' => '***', 'address' => 'Test 123', 'city' => '*******', 'country' => '******', 'pincode' => '*****', 'mobile' => '**********', 'status' => 1],
];
DeliveryAddress::insert($deliveryRecords);
}
}
I expect DeliveryAddress migration will have the columns with the $table->id(); declaration. In this case your seeder when run more than once will insert duplicate value in the id column.
Either you can truncate the table before running your seeder .
or you can skip inserting the value for id column since it is auto increment value.
Also, confirm if the user_id column is having any foreignkey constraint. If yes you should have a record for users table with id "1" to avoid any bad data.