2

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

  1. Add Database\Seeders namespace at top of DatabaseSeeder.php and other Seeder files

  2. Replace folder name seeds to seeders located at \database\ folder

  3. Update composer.json like below:

    "autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
    

    },

  4. 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);
    }
}
Muqadar Ali
  • 57
  • 2
  • 11
  • Can you show your DeliveryAddressTableSeeder seeder? and are there any data in that table? – Farhan Ibn Wahid Apr 15 '21 at 00:40
  • @Psycho please checkout. i have update the question – Muqadar Ali Apr 15 '21 at 06:27
  • 1
    If I understand seeding correctly, you're just meant to run it once when your tables are all created, once the database is seeded you shouldn't need to run it again unless you refresh the database. – apokryfos Apr 15 '21 at 06:42
  • @MuqadarAli, don't you want to truncate your table first before seeding? Your delivery address has an id set to 1. I guess that this ID is set to be incremental in your table. I think that you should either truncate your table, or remove the "id" field from the seeder. – Romain biard Apr 15 '21 at 06:42
  • @Muqadar according to your seeder, Jaswinder's answer should be sufficient for you – Farhan Ibn Wahid Apr 15 '21 at 08:20

2 Answers2

0

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 .

    <?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\DeliveryAddress;

class DeliveryAddressTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DeliveryAddress::truncate();
        //
        $deliveryRecords = [
            ['id' => 1, 'user_id' => 1, 'name' => '***', 'address' => 'Test 123', 'city' => '*******', 'country' => '******', 'pincode' => '*****', 'mobile' => '**********', 'status' => 1], 
        ];

        DeliveryAddress::insert($deliveryRecords);
    }
}

or you can skip inserting the value for id column since it is auto increment value.

 public function run()
  {
        
        $deliveryRecords = [
                ['user_id' => 1, 'name' => '***', 'address' => 'Test 123', 'city' => '*******', 'country' => '******', 'pincode' => '*****', 'mobile' => '**********', 'status' => 1], 
            ];
    
        DeliveryAddress::insert($deliveryRecords);
  }

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.

Jaswinder Singh
  • 731
  • 5
  • 20
-1
        Eloquent::unguard();

        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        $this->call('YourTTableSeeder');

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

Throw these in your seeders

Michael Mano
  • 3,339
  • 2
  • 14
  • 35