[英]SQLSTATE[23000]: Integrity constraint violation: 1062
User::updateOrCreate([
'identifier' => $user->id,
'username' => $user->nickname,
'name' => $user->name,
'avatar' => $user->avatar,
'visibility' => $user->visibility,
'api_token' => Uuid::generate()
]);
And my migration: 我的迁移:
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();
});
But results in a QueryException :( 但结果是QueryException :(
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '76561198364059468' for key 'users_identifier_unique'
SQLSTATE [23000]:完整性约束违规:1062重复条目'76561198364059468'用于键'users_identifier_unique'
This error says that there is a row in the users
table where identifier
has value of 76561198364059468
already. 此错误表示
users
表中有一行identifier
值已经为76561198364059468
。 So just use an unique identifier to avoid the error. 因此,只需使用唯一标识符即可避免错误。
Do like this 这样做
User::updateOrCreate(
[
'identifier' => $user->id
],
[
'username' => $user->nickname,
'name' => $user->name,
'avatar' => $user->avatar,
'visibility' => $user->visibility,
'api_token' => Uuid::generate()
]
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.