DEV Community

Cover image for Triggers in Supabase for making new rows.
Next Dev
Next Dev

Posted on

Triggers in Supabase for making new rows.

Hi šŸ‘‹ friends. This blog is made for teaching you how to set up triggers in supabase. You can use triggers for sending notifications or adding more rows to your tables.

šŸŽÆGoals

One day, I logged into your site and registered there. Your website allows me to add my details as Facebook does. But the problem is that you cannot add details in auth.users the table in supabase.

Relax! I will tell you what is schema and tables in Postgres(It is a database that supabase provides).

In database, there are many schemas **eg(auth, public or more). Then schemas has **tables **eg(posts, authors, comments) then tables has **rows associated with columns eg(name, created_at)

In simple diagram šŸ‘‡

In this situation, you need to create a row in public the table so that you can fetch the userā€™s details from supabase.

šŸ¤“Relax!!! Itā€™s simple to implement.

First, letā€™s setup profiles a table in your project.

Remember that we need to connect public.profile.id to auth.users.id Here, public and auth are schemas ,and profile and users are tables.

Then, you need to your functions in database settings. You can go there by clickingšŸ‘‡

Now, click on create new functions in the top right:

CodešŸ‘‡

begin

insert into public.profiles(id, email)

values(new.id, new.email);

return new;

end;
Enter fullscreen mode Exit fullscreen mode

Now letā€™s go to **triggers **in database settings.

Then click on confirm.

Now, letā€™s check it.

Boom. Now we have users in the profiles table. But thereā€™s a bug.

When we try to delete the user from auth table. we receive

So, first, go to the profile table and delete it ā›”.

Then go to SQL editor

Code:

create table public.profiles(

id uuid primary key references auth.users(id) on update cascade on delete cascade,

email text not null

);
Enter fullscreen mode Exit fullscreen mode

Now, you can delete the user from auth table and it will also get
deleted from profileā€™s table.

For people who understand Hindi:
%[https://www.youtube.com/watch?v=VbfU7KRXdKM]

Top comments (0)