【问题标题】:Unable to add foreign key in Supabase无法在 Supabase 中添加外键
【发布时间】:2022-10-25 05:37:21
【问题描述】:

我正在关注 Jon Meyers 的“使用 Next.js、Supabase 和 Stripe 构建 SaaS 产品”课程。我在使用外键在 Supabase 中添加表之间的关系部分中遇到问题。我创建了一个名为 profile 的表,其中 id 和 created_at 是自动生成的列,而 is_subscribed 和间隔是由我定义的,我必须添加与 auth.users 表的外键关系以及 profile 表的 id 列,我猜它是由管理的Supabase 在引擎盖下。有人可以帮我解决这个问题。谢谢

【问题讨论】:

    标签: javascript sql postgresql supabase supabase-database


    【解决方案1】:

    主要问题是您创建了 ID 为 bigint 而不是 UUID 的配置文件表。您可以在 SQL 编辑器中使用以下命令更改此设置:

    -- Dropping the primary key to change it:
    ALTER TABLE public.profiles 
    DROP CONSTRAINT profile_pkey;
    --Dropping & recreating the column as UUID:
    ALTER TABLE profiles
    DROP COLUMN id;
    ALTER TABLE profiles
    ADD COLUMN id uuid;
    --Adding the primary key back:
    ALTER TABLE public.profiles 
    ADD PRIMARY KEY (id);
    -- Setting the foreign key relationship:
    ALTER TABLE public.profiles 
    ADD FOREIGN KEY (id) REFERENCES auth.users(id)
    

    【讨论】:

    • 嗨@Mansueli,感谢您的回复。我试图执行这些 SQL 查询。前 2 行已成功执行。但是接下来的 2 行出现错误 ALTER TABLE profile ALTER COLUMN id TYPE uuid; Failed to run sql query: identity column type must be smallint, integer, or bigint
    • 您需要删除该列并将其重新创建为 UUID。我会更新答案。
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多