SQLSTATE [23000]:完整性约束违规:1062

时间:2017-03-26 23:03:15

标签: php mysql sql laravel

User::updateOrCreate([
    'identifier' => $user->id,
    'username' => $user->nickname,
    'name' => $user->name,
    'avatar' => $user->avatar,
    'visibility' => $user->visibility,
    'api_token' => Uuid::generate()
]);

我的迁移:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('identifier')->unique();
    $table->string('username')->unique();
    $table->string('name');
    $table->string('avatar');
    $table->string('trade')->nullable();
    $table->decimal('funds')->default(0);
    $table->enum('visibility', [1, 2, 3]);
    $table->uuid('api_token');
    $table->timestamps();
});

但导致QueryException :(

  

SQLSTATE [23000]:完整性约束违规:1062重复条目   ' 76561198364059468'关键字' users_identifier_unique'

2 个答案:

答案 0 :(得分:0)

此错误表示users表中有一行identifier已经有76561198364059468的值。因此,只需使用唯一标识符即可避免错误。

答案 1 :(得分:0)

这样做

User::updateOrCreate(
    [
        'identifier' => $user->id
    ],
    [
        'username' => $user->nickname,
        'name' => $user->name,
        'avatar' => $user->avatar,
        'visibility' => $user->visibility,
        'api_token' => Uuid::generate()
    ]
);