gpt4 book ai didi

mysql - Laravel - 违反完整性约束 : 1062 Duplicate entry

转载 作者:可可西里 更新时间:2023-11-01 07:49:31 24 4
gpt4 key购买 nike

当我尝试在我的数据库中创建一个新用户时遇到问题,如果我在 phpmyadmin 中添加用户我没有问题,但是当我尝试在我的 laravel web 中创建用户时,用户已创建并出现此错误:SQLSTATE [23000]:违反完整性约束:1062 键“PRIMARY”的重复条目“15.236.964-5”我不知道是哪个问题,因为在数据库中不存在相同的主键。有问题的表是:

CREATE TABLE IF NOT EXISTS `users` (
`rut` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` char(60) COLLATE utf8_unicode_ci NOT NULL,
`edad` int(11) NOT NULL,
`pais` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`comuna` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`sector` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`tipo` enum('profesor','alumno','administrador_parrilla','administrador_sistema','externo') COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `users`
ADD PRIMARY KEY (`rut`),
ADD UNIQUE KEY `usuario_rut_unique` (`rut`),
ADD UNIQUE KEY `usuario_email_unique` (`email`);

我的 Controller :

class UsuarioController extends Controller
{
public function index(){
$users = User::All();
return view('usuario.index', compact('users'));
}

public function create(){
return view('usuario.create');
}
public function store(Request $request) {
User::create([
'name' => $request['name'],
'rut' => $request['rut'],
'email' => $request['email'],
'password' => bcrypt($request['password']),
'edad' => $request['edad'],
'pais' => $request['pais'],
'comuna' => $request['comuna'],
]);
User::create($request->all());
return redirect('/usuario')->with('message', 'store');
}

public function edit($rut){
$user = User::find($rut);
return view ('usuario.edit',['user'=>$user]);
}

public function update($id, Request $request){
$user = User::find($id);
$user -> fill($request->all());
$user -> save();

Session::flash('message', 'Usuario Editado Correctamente');
return Redirect ('/usuario');
}
}

我的模型:

class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = "users";
protected $fillable = [
'rut', 'name', 'email', 'password', 'edad', 'pais', 'comuna', 'sector', 'tipo',
];
/**
protected $primaryKey = 'rut';


* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}

我无法更改添加 auto_increment 的主键,因为我使用“Rut”作为主键,它是智利人的身份代码。

最佳答案

执行以下步骤:

1) 删除ADD UNIQUE KEY usuario_rut_unique (rut)

因为您已经定义了 rut 是主键,所以 PK 始终是唯一的(这是 PK 的行为)

2) 在你的模型中定义 rut 是主键,并禁用自动递增,因为它是 varchar(不是 int 不能自然迭代):

/**
* primaryKey
*
* @var integer
* @access protected
*/
protected $primaryKey = 'rut';
/*
You cannot comment it to make in disabled, it will look for `id` param by default.
You cannot set it to be = null because in Your code You use ::find() function
that looks for primaryKey.
*/

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

3) 删除这一行:

User::create($request->all());

因为您已经在上一行中将记录插入数据库。

关于mysql - Laravel - 违反完整性约束 : 1062 Duplicate entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com