Laravel播种错误SQLSTATE [23000]

时间:2017-06-24 17:06:46

标签: php database laravel laravel-5 laravel-5.2

当我尝试为我的数据库播种时,但是我收到一条错误消息,说我的密钥'users_class_id_unique'有一个重复的条目'1'。

有谁知道造成这个问题的原因是什么?

这就是错误的样子

[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' 
for key 'users_class_id_unique' (SQL: insert into `users` (`name`, 
`lastname`, `email`, `class_id`, `password`
`role_id`, `created_at`, `updated_at`) values (Example, Name, 
example@outlook.com, 1, 
$2y$10$gqFXkYt9LRoCH6LqsosEPOI/2t.VDoNzBgV3x/przh.1KiuJqvRwe, 2, 1971-06-20 
06:10:44, 1975-03-0
9 17:46:23))



[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' 
for key 'users_class_id_unique'

这是我的播种文件

<?php

use Faker\Factory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $faker = Faker\Factory::create();

    //Zeker weten dat de eerste user één admin is
    DB::table('users')->insert([
        'name' => 'Rainier',
        'lastname' => 'Laan',
        'email' => 'rainier.laan@home.nl',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Collin',
        'lastname' => 'Nieuw Beerta',
        'email' => 'collinweetnietwelkeemail@hotmail.com',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 1,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Godpieter',
        'lastname' => 'alwin',
        'email' => 'Godpieter@Allmightygodpieter.com',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    // Onderste code maakt 8 random users aan met het wachtwoord 'Welkom'

    foreach(range(1,7) as $index) {
        DB::table('users')->insert([
            'name' => $faker->name,
            'lastname' => $faker->lastName,
            'email' => $faker->email,
            'class_id' => rand(1, 3),
            'password' => bcrypt('welkom'),
            'role_id' => 1,
            'created_at' => $faker->dateTime,
            'updated_at' => $faker->dateTime,
        ]);
    }
}
}

班级模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Classes extends Model
{
protected $fillable = [
    'class_name', 'subject_id',
];

public function subjects() {
    return $this->hasMany('App\Subject');
}

public function users() {
    return $this->hasMany('App\User');
}


}

CLASS Migration

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateClassesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('classes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('subject_id');
        $table->string('class_name')->unique();
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
}
}

2 个答案:

答案 0 :(得分:0)

实际上你不能插入'class_id'=&gt;两个用户都是1。此类ID应该是唯一的。 它在您的模型文件中定义。 如果你想插入相同的'class_id',那么改变你的模型和db或者其他方面改变你的'class_id'之一。 如: 'class_id'=&gt; 1, 'class_id'=&gt; 2,

<?php

use Faker\Factory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $faker = Faker\Factory::create();

    //Zeker weten dat de eerste user één admin is
    DB::table('users')->insert([
        'name' => 'Rainier',
        'lastname' => 'Laan',
        'email' => 'rainier.laan@home.nl',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Collin',
        'lastname' => 'Nieuw Beerta',
        'email' => 'collinweetnietwelkeemail@hotmail.com',
        'class_id' => 2,
        'password' => bcrypt('welkom'),
        'role_id' => 1,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Godpieter',
        'lastname' => 'alwin',
        'email' => 'Godpieter@Allmightygodpieter.com',
        'class_id' => 3,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    // Onderste code maakt 8 random users aan met het wachtwoord 'Welkom'

    foreach(range(4,10) as $index) {
        DB::table('users')->insert([
            'name' => $faker->name,
            'lastname' => $faker->lastName,
            'email' => $faker->email,
            'class_id' => rand(1, 3),
            'password' => bcrypt('welkom'),
            'role_id' => 1,
            'created_at' => $faker->dateTime,
            'updated_at' => $faker->dateTime,
        ]);
    }
}
}

答案 1 :(得分:0)

问题出在迁移中。您的专栏class_id设置为唯一,但您继续指定相同的值'class_id' => 1。将其更改为

public function up()
{
    Schema::create('classes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('subject_id');
        $table->string('class_name');
        $table->rememberToken();
        $table->timestamps();
    });
}

并重新运行迁移。还要确保在users表中更改它。

php artisan migrate:refresh --seed

您必须弄清楚自己是否需要该列是唯一的。如果它必须是唯一的,则必须更改播种机,以确保它仅为class_id生成唯一值。

如果无法重新运行迁移,请按以下方式更改播种机:

class UsersTableSeeder extends Seeder
{
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $faker = Faker\Factory::create();

    //Zeker weten dat de eerste user één admin is
    DB::table('users')->insert([
        'name' => 'Rainier',
        'lastname' => 'Laan',
        'email' => 'rainier.laan@home.nl',
        'class_id' => 1,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Collin',
        'lastname' => 'Nieuw Beerta',
        'email' => 'collinweetnietwelkeemail@hotmail.com',
        'class_id' => 2,
        'password' => bcrypt('welkom'),
        'role_id' => 1,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    DB::table('users')->insert([
        'name' => 'Godpieter',
        'lastname' => 'alwin',
        'email' => 'Godpieter@Allmightygodpieter.com',
        'class_id' => 3,
        'password' => bcrypt('welkom'),
        'role_id' => 2,
        'created_at' => $faker->dateTime,
        'updated_at' => $faker->dateTime,
    ]);

    // Onderste code maakt 8 random users aan met het wachtwoord 'Welkom'

    foreach(range(4,10) as $index) {
        DB::table('users')->insert([
            'name' => $faker->name,
            'lastname' => $faker->lastName,
            'email' => $faker->email,
            'class_id' => $index,
            'password' => bcrypt('welkom'),
            'role_id' => 1,
            'created_at' => $faker->dateTime,
            'updated_at' => $faker->dateTime,
        ]);
    }
}

如果您的class_id需要为1,2或3,请使用播种机。