简体   繁体   中英

laravel 8 seeding , SQLSTATE[23000]: Integrity constraint violation:

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);
    }
}

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.

        Eloquent::unguard();

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

        $this->call('YourTTableSeeder');

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

Throw these in your seeders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM