【发布时间】:2022-11-10 23:05:12
【问题描述】:
对于我的网站(由 Supabase 提供后端支持),我有内置的 auth.users 表和一个包含电子邮件、用户名和学校文本字段的 public.profiles 表。我创建了一个触发器和函数,当在auth.users 中插入新行时,将使用电子邮件作为电子邮件,将全名作为电子邮件的用户名组件(myName 用于myName@email.com)在 public.profiles 中插入一行, 并从 public.college_emails 表中查询大学名称以确定学校(如果某人使用域“@uci.edu”的电子邮件注册,它将在配置文件@987654326 中插入“加州大学欧文分校” @字段。如果无法匹配到学校的域,它将插入“其他”。
例子:
假设在我的网站上注册了电子邮件“rickAstley@ucla.edu”的用户。将此行添加到
auth.users后,触发器应执行一个函数,该函数将使用以下字段向public.profiles添加新行:full_name:rickAstley,school: 加州大学洛杉矶分校。这是该函数的当前 Postgres 代码,由
auth.users上的插入触发器执行:create or replace function public.handle_new_user() returns trigger as $$ declare coll text ; begin SELECT college INTO coll FROM college_emails WHERE tag = SPLIT_PART(new.email, '@', 2); IF NOT FOUND THEN coll = 'Other' ; END IF ; INSERT INTO public.profiles (id, email, full_name, school, created_at) values (new.id, new.email, SPLIT_PART(new.email, '@', 1), coll, current_timestamp) ; return new; end; create trigger on_new_user_created after insert on auth.users for each row execute procedure public.handle_new_user();当我使用虚构的 uuid 和学校电子邮件直接插入
auth.users时,例如:insert into auth.users (id, email) values ('d1d19367-3d0b-d497-9191-18a9d8e37850','myLongName@ucr.edu')触发器和功能按预期完美执行。但是,如果我尝试以标准方式将用户添加到
auth.users,通过以访客身份访问网站并注册,我会收到错误消息Database Error Saving New User.这是图像,尽管我怀疑它会有所帮助:我不明白为什么这段代码在直接插入时可以正常工作,但在使用 Supabase 的 Auth 组件插入时却不行。这是创建注册功能的代码:
<Auth supabaseClient = {supabase} socialLayout = "horizontal" socialButtonSize = "xlarge" />另一件需要注意的事情是当我使用一个函数时才不是在函数中执行查询,例如在第一个句点之前插入电子邮件域的第一部分而不是在
public.college_emails中查找它,它工作正常(例如,使用域“@uci.edu”注册将插入“ uci" 进入school字段,而不是查询全名)。这就是工作功能:
create or replace function public.handle_new_user() returns trigger as $$ begin insert into public.profiles (id, email, full_name, school, created_at) values (new.id, new.email, SPLIT_PART(new.email, '@', 1), SPLIT_PART(new.email, '@', 2) ,current_timestamp); return new; end; $$ language plpgsql security definer;我渴望在这个问题上的任何线索,非常感谢提前。
【问题讨论】:
-
为什么不在 github 上的 supabase 讨论页面中提问?我面临着类似的问题。 :(
标签: sql postgresql authentication triggers supabase