[Supabase] Use Triggers to Automatically Update Your Supabase Tables

Documentation on creating public user tables has been moved here. Supabase has also removed triggers from this doc but you can still use them.

In this lesson, we'll use Supabase's triggers functionality to automatically update data in our application when a user creates an account. Triggers are a powerful functionality of relational databases that allow us to minimize the amount of manual development work we need to do to build applications.

 

Idea is when user signup, will save to default auth.users table, we want to automaticlly add to our own public.user table as well, with just id information

-- insert a row into public.user
create function public.handle_new_user()
returns trigger as $$
begin
  insert into public.user (id)
  values (new.id);
  return new;
end;
$$ language plpgsql security definer;

-- triger the function every time a user is created
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

 

posted @ 2021-08-20 14:55  Zhentiw  阅读(87)  评论(0编辑  收藏  举报