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

Supabase无法添加外键:关联profile与auth.users表问题求助

解决Supabase中profile表与auth.users表的外键关联问题

方法一:用SQL语句直接操作(高效可靠)

  • 先确保profile表的id列类型和auth.users的id一致(均为uuid),且设置为非空:
ALTER TABLE profile ALTER COLUMN id TYPE uuid;
ALTER TABLE profile ALTER COLUMN id SET NOT NULL;
  • 创建外键约束:
ALTER TABLE profile
ADD CONSTRAINT profile_id_fkey FOREIGN KEY (id)
REFERENCES auth.users(id)
ON DELETE CASCADE; -- 可选配置:当auth.users中的用户被删除时,自动删除对应profile记录
  • 验证约束是否创建成功:
SELECT conname, conrelid::regclass, confrelid::regclass
FROM pg_constraint
WHERE conrelid = 'profile'::regclass AND contype = 'f';

方法二:用Supabase可视化界面操作(对应截图界面)

  • 打开Supabase控制台,进入profile表的「Constraints」标签页
  • 点击「Add foreign key」按钮
  • 在弹窗中配置:
    • 「Column」选择profile表的id列
    • 「References schema」选auth,「References table」选users,「References column」选id
    • 按需设置「On delete」选项(推荐选CASCADE,实现用户删除时同步清理profile数据)
  • 点击「Save」完成配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 22:35:22