Triggers in Supabase for making new rows.

Triggers in Supabase for making new rows.

·

2 min read

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;

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

);

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

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

Did you find this article valuable?

Support Next Dev by becoming a sponsor. Any amount is appreciated!