【问题标题】:duplicate key value violates unique constraint "core_user_phone_number_client_key" DETAIL: Key (phone_number, clientid)=(b9e507949695) already exists重复键值违反唯一约束“core_user_phone_number_client_key”详细信息:键(电话号码,客户端 ID)=(b9e507949695)已存在
【发布时间】:2022-01-14 13:22:16
【问题描述】:
我在注册时使用 Cognito 对用户进行身份验证,一旦用户单击创建帐户,它应该被定向到验证屏幕(号码和电子邮件),但我的用户却面临此错误
duplicate key value violates unique constraint "core_user_phone_number_client_key" DETAIL: Key (phone_number, clientid)=(b9e507949695) already exists.
我不太确定此错误是否仅与 Cognito 或数据库(Postgres)有关,因为我看不到表上的记录,但是当我尝试使用相同的被拒绝电子邮件创建帐户时,用户说已经存在但是什么时候尝试注册它的说用户不存在(太棘手了)
【问题讨论】:
标签:
postgresql
amazon-web-services
authentication
amazon-cognito
【解决方案1】:
这肯定看起来像 PostgreSQL 错误,除了键有两列的部分,但数据只显示一列。
通常会导致此错误的原因是一个事务尝试两次插入相同的数据,因此与自身发生冲突。由于事务回滚,两行都消失了。因此,外部观察者永远无法发现违规数据,因为它永远不会以提交(可见)的形式存在。
【解决方案2】:
来自同事,所以这将解决问题
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;')
]