You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Supabase更新用户账户手机号失败,报identities_pkey唯一约束重复错误

解决Supabase绑定手机号时的唯一键冲突错误

错误原因

duplicate key value violates unique constraint "identities_pkey" 错误说明你要绑定的手机号已经被其他Supabase用户账户作为身份记录存在。调用updateUser({phone: ...})时,Supabase会尝试为当前用户创建一条新的手机号身份记录,但如果该手机号已被其他用户绑定(比如之前用手机号注册过账户),就会触发唯一键约束冲突。

解决方案

1. 先校验手机号是否已被占用

在调用updateUser之前,先查询auth.users表检查该手机号是否已被其他用户使用。注意要保证查询的手机号格式和你准备传入updateUser的格式完全一致(比如带国际区号的标准格式,无空格或特殊字符)。

2. 处理重复绑定场景

如果当前用户已经绑定过该手机号,直接提示成功即可,无需重复调用接口。

修改后的核心代码示例

try {
  const formattedPhone = phoneNumber.formatInternational().replace(/\s/g, ''); // 移除空格,确保格式统一

  // 先查询该手机号是否已被其他用户绑定
  const { data: existingUsers, error: queryError } = await supabaseClient
    .from('auth.users')
    .select('id')
    .eq('phone', formattedPhone)
    .neq('id', user.id); // 排除当前用户

  if (queryError) throw queryError;

  if (existingUsers.length > 0) {
    setError("This phone number is already in use.");
    setIsModalOpen(true);
    setLoading(false);
    return;
  }

  // 检查当前用户是否已经绑定该手机号
  const { data: currentUser } = await supabaseClient.auth.getUser();
  if (currentUser.user?.phone === formattedPhone) {
    setSuccess("Mobile number is already bound to your account!");
    router.push("/account");
    setLoading(false);
    return;
  }

  // 执行绑定操作
  let { data, error } = await supabaseClient.auth.updateUser({
    phone: formattedPhone,
  });

  if (error) throw error;

  console.log("Update success, redirecting");
  router.push("/account");
  setSuccess("Mobile number updated successfully!");
  console.log("user new updated:", user, "data:", data);
} catch (error: unknown) {
  if (error instanceof Error) {
    setError("Error updating user mobile number: " + error.message);
    setIsModalOpen(true);
    console.log("Error caught: ", error.message);
  } else {
    console.log("Error caught: ", error);
  }
} finally {
  setLoading(false);
}

额外注意事项

  • 手机号格式统一:确保传入的手机号是标准格式(比如+1234567890),避免因格式不一致导致查询不到已存在的记录,或者绑定失败。
  • 权限配置:确保你的Supabase服务角色有权限查询auth.users表。如果默认权限不足,可以在Supabase控制台的SQL编辑器中执行以下语句开放权限:
GRANT SELECT ON auth.users TO authenticated;

内容的提问来源于stack exchange,提问作者LeCoda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 19:10:11