Triggers in Supabase for making new rows.
Tags: supabase, triggers, nextjs, postgres
Published: 2022-05-23 06:11:53

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 👇

![](https://cdn-images-1.medium.com/max/3840/1*xL2eMvdmPxk2GwYXYebySg.jpeg)

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

![](https://cdn-images-1.medium.com/max/3840/1*hERetB--iGw3UqaGV20yUQ.jpeg)

🤓Relax!!! It’s simple to implement.

First, let’s setup profiles a table in your project.

![](https://cdn-images-1.medium.com/max/3840/1*xOhxp2TXRZplSCfXka0uTg.png)

![](https://cdn-images-1.medium.com/max/3840/1*62WeqcxdcvGtx6Qk_bwP2w.png)

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.*

![](https://cdn-images-1.medium.com/max/3840/1*ow6cUc4ZmtTKzF_JElPMGg.png)

![](https://cdn-images-1.medium.com/max/3840/1*KhNGcWu3zoXCtwdzDeq1dA.png)

Then, you need to your functions in database settings. You can go there by clicking👇

![](https://cdn-images-1.medium.com/max/3840/1*p1yJ54rrLIYpWM5CyKKWcw.png)

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

![](https://cdn-images-1.medium.com/max/3840/1*voxlQYlUvtMOE7W_WHO3Lg.png)

![](https://cdn-images-1.medium.com/max/3840/1*OS-HozN0w1kA6PKgGIdgGw.png)

![](https://cdn-images-1.medium.com/max/3840/1*22kUfVHmJ4gIaQnJX3eRpg.png)

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.

![](https://cdn-images-1.medium.com/max/3840/1*vC4NNcNYoc9yW3jHOlwOJA.png)

![](https://cdn-images-1.medium.com/max/3840/1*EE97zj95k3WgXfJCoNXA5w.png)

![](https://cdn-images-1.medium.com/max/3840/1*OgparfnEpaga3gNbdjrxuw.png)

![](https://cdn-images-1.medium.com/max/3840/1*WKZNNtROTnqNYxsFQ5LLxA.png)

![](https://cdn-images-1.medium.com/max/3840/1*ijw4rSM-X2_S1fQiZ2RXnQ.png)

Then click on confirm.

Now, let’s check it.

![](https://cdn-images-1.medium.com/max/3840/1*XlkwtfaFvaJaI-x0J0tInw.png)

![](https://cdn-images-1.medium.com/max/3840/1*kctjEYQVW4yz3LJ9VfY5Dg.png)

![](https://cdn-images-1.medium.com/max/3840/1*XBhiRliVZL3BP6QidDa6CA.png)

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 

![](https://cdn-images-1.medium.com/max/3840/1*GQWoGMH53DHm-_fkZIXrIw.png)

![](https://cdn-images-1.medium.com/max/3840/1*mYxf8Ai9D46aNq7QTCe8nw.png)

So, first, go to the profile table and delete it ⛔.

Then go to SQL editor

![](https://cdn-images-1.medium.com/max/3840/1*rpMLI0z_1qe0NQXE-TKK7g.png)

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:
%[https://www.youtube.com/watch?v=VbfU7KRXdKM]