php - SQLSTATE[23000] : Integrity constraint violation: 1062

标签 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]: Integrity constraint violation: 1062 Duplicate entry '76561198364059468' for key 'users_identifier_unique'

最佳答案

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

关于php - SQLSTATE[23000] : Integrity constraint violation: 1062,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035594/

相关文章:

sql - 将原始 SQL 结果映射到 .NET Core 中的对象列表

php - 如何将两个选择查询合并到一个查询中

php - 在 header php 中使用 x-auth-token 发送请求

php - 如何将字符串和数据从 iOS 发送到 Php 以在 MySQL 数据库中创建表

php - Laravel 中的模型工厂和数据库播种机有什么区别?

MySql 简单查询性能 - 100 万行

php - 如何使用 symfony 更新 mysql 列

php - 单击链接时进行 Sql 更新

php - 从循环创建的表单中检索多行...卡住了

sql - 在列名称中包含空格的Proc SQL