重复的键值违反了唯一约束"core_user_phone_number_client_key"详细信息:键(phone_number,客户端 ID)=(b9e507949695) 已存在



我在注册时使用Cognito对用户进行身份验证,一旦用户点击创建帐户,它应该被引导到验证屏幕(号码和电子邮件(,但我的用户却面临这个错误

duplicate key value violates unique constraint "core_user_phone_number_client_key" DETAIL: Key (phone_number, clientid)=(b9e507949695) already exists.

我不太确定这个错误是否只与Cognito或数据库(Postgres(有关,因为我看不到表上的记录,但当我尝试创建一个带有相同拒绝电子邮件的帐户时,它说用户已经存在,但何时尝试注册它说用户不存在(这太棘手了(

这看起来确实像PostgreSQL错误,除了键有两列,但数据只显示一列的部分。

通常会导致此错误的原因是,一个事务试图插入相同的数据两次,因此与自身发生冲突。由于事务回滚,所以两行都消失了。因此,外部观察者永远无法发现有问题的数据,因为它从未以提交(可见(的形式存在

来自同事,因此这将解决问题

operations = [
migrations.RunSQL('CREATE UNIQUE INDEX IF NOT EXISTS core_user_username_key ON core_user (username);'),
# Make sure we take into account related client's id for unique index, to limit uniqueness
# verification by client profiles set (several clients may have profiles with the same email & phone number)
migrations.RunSQL('CREATE UNIQUE INDEX IF NOT EXISTS core_user_email_client_key '
'ON core_user (email, client_id) WHERE is_active = TRUE;'),
migrations.RunSQL('CREATE UNIQUE INDEX IF NOT EXISTS core_user_phone_number_client_key '
'ON core_user (phone_number, client_id) WHERE is_active = TRUE;')
]

最新更新