简体   繁体   English

laravel 8 播种,SQLSTATE[23000]:违反完整性约束:

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

I have undegraded my project from Laravel 7 to 8 .我已将我的项目从 Laravel 7 降级为 8 but the problem is when i run the seeding command php artisan db:seed it shows an error SQLSTATE[23000]: Integrity constraint violation: .但问题是当我运行播种命令php artisan db:seed它显示错误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在 DatabaseSeeder.php 和其他 Seeder 文件顶部添加 Database\Seeders 命名空间

  2. Replace folder name seeds to seeders located at \database\ folder将文件夹名称种子替换为位于 \database\ 文件夹的播种机

  3. Update composer.json like below:更新 composer.json 如下:

     "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 php 工匠数据库:种子

I have also change the DatabaseSeeder command $this->call(DeliveryAddressTableSeeder::class);我还更改了DatabaseSeeder命令$this->call(DeliveryAddressTableSeeder::class);

DeliveryAddressTableSeeder 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();我希望 DeliveryAddress 迁移将具有 $table->id(); 的列declaration.宣言。 In this case your seeder when run more than once will insert duplicate value in the id column.在这种情况下,您的播种机在多次运行时会在 id 列中插入重复值。

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.或者您可以跳过插入 id 列的值,因为它是自动递增值。

 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.此外,确认 user_id 列是否有任何外键约束。 If yes you should have a record for users table with id "1" to avoid any bad data.如果是,您应该有一个用户表的记录,ID 为“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把这些扔进你的播种机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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