Using PostgreSQL triggers to automate processes with Supabase

Поділитися
Вставка
  • Опубліковано 13 жов 2024

КОМЕНТАРІ • 72

  • @selique
    @selique 2 роки тому +18

    dude if you knew how much the community racked their brains over this authoring feature...

  • @deoarlo
    @deoarlo 6 місяців тому +4

    note that this is 2 years ago. now you must create a trigger with SQL editor if you listen to auth

  • @yoapps137
    @yoapps137 Рік тому +7

    I appreciate the way you are also describing, the options on the path you arent taking... it helps because when a developer really sits down to make his scratch they come across the same options and wonder "...but what might this do?"... and you have already prepped us for that. thanks

  • @__joellee__
    @__joellee__ 2 роки тому +12

    0:47 -- Application introduction
    1:32 -- Auth settings configuration
    2:11 -- Github Repo
    3:43 -- Overview of the profiles table to store additional user data
    5:07 -- Adding a column to store click count
    5:40 -- Adding a trigger
    6:09 -- Adding a function which takes effect on the trigger
    6:50 -- Description of the "new" value
    7:25 -- Changing security roles and rationale(Change to security Definer)
    8:55 -- Explanation of trigger types(Row vs Statement)
    10:17 -- Supabase Twitter and Discord

  • @hu3m4n90
    @hu3m4n90 2 роки тому +19

    Please keep this going!! We need this beautiful level of documentation.
    Im loving supabase! :)
    Really grateful for it!!

    • @JonMeyers
      @JonMeyers 2 роки тому +1

      That awesome! Really glad to hear you're enjoying it!

  • @ojukwuchinonye808
    @ojukwuchinonye808 3 дні тому

    Thank you so much for this!!! I literally cried last night😂😂😂😂

  • @anthonyngooo
    @anthonyngooo Рік тому +9

    8:05 This part needs to be updated. As of sometime in October 2023, the auth schema is available, but the users table is not visible and only public tables are shown

    • @dnserror89
      @dnserror89 8 місяців тому

      You can still access the users table using the SQL editor. They only removed access direectly from the UI.

    • @antgoesafk
      @antgoesafk 8 місяців тому

      @@dnserror89that is correct, I made a video on it too aha

  • @Broski_Rodragweez
    @Broski_Rodragweez 7 місяців тому +1

    I almost scoffed at the idea of using Supabase because for one I don't care much for SQL (got burned out on it a few years ago), for two I don't care for putting business logic in my database. BUT, since it appears that I can build apps without all the API layer boilerplate I am starting to come around. Plus having the AI available to help write sql makes it less painful.

  • @ProfRa6574
    @ProfRa6574 6 місяців тому +2

    The table to fire the trigger (auth table) does not show up in my list.

    • @AmxCsifier
      @AmxCsifier 3 місяці тому +1

      same issue here. only the public schema tables are displayed!

    • @AmxCsifier
      @AmxCsifier 3 місяці тому

      people are saying that the trigger must be created using sql code instead

  • @yellowsubmarine12321
    @yellowsubmarine12321 2 роки тому +3

    Every time I try this I get a "Database error saving new user" 500 error response. I've rewatched many times now and I'm doing exactly what you are. Any ideas?

  • @loribryant4999
    @loribryant4999 8 місяців тому +2

    He please upddate this video there is no auth autho option anymore in trigger

  • @ofeenee
    @ofeenee Рік тому +2

    Please make more videos like these on triggers and plpgsql! 🙏🏼🙏🏼🙏🏼

  • @lararawf6100
    @lararawf6100 6 днів тому

    Thanks man how to depend on specific data add another profile table?
    ex: when the user creates an account, check if the employee will save in the employee table and otherwise will save the manager tabel?

  • @tattran2263
    @tattran2263 2 роки тому +3

    it's exactly what I'm looking for, thank you!

  • @zb1921
    @zb1921 Рік тому +6

    It's no longer possible to create the trigger through the UI, but I figured out how to create the trigger in the SQL editor.
    create trigger "create_profile_for_user_trigger"
    after insert on auth.users
    for each row
    execute function create_profile_for_user();

    • @Guido_EFOMM
      @Guido_EFOMM 11 місяців тому

      Nice! It worked! Thank you.

    • @clojuregang3618
      @clojuregang3618 11 місяців тому

      Thank you, I wasted hours messing with this....

    • @elan2199
      @elan2199 10 місяців тому

      Thank you!

    • @samifouad
      @samifouad Місяць тому

      tutorials needs to update to explain this UI change, thanks 🙏🏾

  • @candrad.s9504
    @candrad.s9504 13 днів тому

    dude i want to ask, how to make the trigger work for some conditional time

  • @amoore2922
    @amoore2922 Рік тому +1

    Great video! Saved me a bunch of trial and error. Would like to point out that it seems that Orientation > Row is required if you want access to the "new" object in the function called by the trigger. Perhaps there is another object available in the function if Orientation > Statement is used?

    • @ajaychandrasekaran9262
      @ajaychandrasekaran9262 Рік тому

      did u find a solution to this by any chance

    • @amoore2922
      @amoore2922 Рік тому +1

      @@ajaychandrasekaran9262 Are you looking to use the Orientation > Statement? We're using row for our project anyway, so it was not an issue for us. The supabase support has been very helpful. I'll bet if you create a help ticket they will have an engineer provide an answer.

    • @ajaychandrasekaran9262
      @ajaychandrasekaran9262 Рік тому

      @@amoore2922Yeah, we are using statement. I ended up finding a solution actually, it was using transition tables that were introduced in Postgres v10

  • @evanrosz
    @evanrosz Рік тому

    Thanks for the overview. I pressed the like button twice to send you a very kind message!

  • @fanizul1282
    @fanizul1282 11 місяців тому +1

    Trigger from auth is not available yet, how to trigger when user created?

  • @aahl_work
    @aahl_work 7 місяців тому

    The auth schema is managed by Supabase and is read-only through the dashboard.
    Edit: Use the SQL Editor

  • @JonathanHarford
    @JonathanHarford 9 місяців тому +1

    As many have noted, Supabase no longer allows us to create triggers based on the auth schema. I found a video with the workaround: ua-cam.com/video/mcrqn77lUmM/v-deo.html

  • @PinasPiliNa999
    @PinasPiliNa999 Рік тому

    is it a good practice for a production application for Inventory management to use Triggers? Hope you can make a video to demonstrate inventory management using Triggers. Thanks!

  • @chaoslordi
    @chaoslordi 7 місяців тому +1

    This video could use an update that triggers cannot be created via supabase trigger interface but with an sql query instead. At least that was the case when I followed along today.

  • @Troy-ol5fk
    @Troy-ol5fk 2 роки тому +1

    How to do scheduled publish in Supabase ?

  • @RootsterAnon
    @RootsterAnon Рік тому

    I really wish you finish this video with clicking and reading that value from newly created profile, but I guess it involves more frontend part to do so.

  • @okaycloud
    @okaycloud 2 роки тому +1

    but with this FK in place (in profiles which relates to ID of auth.users), we cant delete a user in the AUTH section anymore because of FK constraint error on delete right?

    • @JonMeyers
      @JonMeyers 2 роки тому

      Correct, you would need to delete the profile first. You could also remove the constraint and add one with cascading delete with raw SQL 👍

  • @MishaMoroshko
    @MishaMoroshko Рік тому

    @JonMeyers @Supabase I'd like to create a profile row only after the user confirms their email. Do I understand correctly that the UPDATE event will call the trigger function on any update, and there is no way to trigger the function on update of a specific column? Ideally, I'd like to trigger the function when `email_confirmed_at` is changing from NULL to a non-NULL value in `auth.users`. Is this possible to achieve this?

  • @leandroosterne
    @leandroosterne Рік тому

    How would the automatic creation of records in a monthly table be, so that every month records were automatically generated in a table?

  • @rahulagarwal968
    @rahulagarwal968 Рік тому

    When we are using email authentication then we can send raw user meta data and fetch inside functions. But How can we get the username and profile image value from functions if we are authenticating with Google ?

  • @0xbinarylol
    @0xbinarylol Місяць тому

    Now only i am getting is can't create new user Data base error. after automating on user insert in auth.users trigger

  • @JustADeveloper-l7x
    @JustADeveloper-l7x Рік тому +1

    7:45 damn i wish this was mentioned in the docs. Took me an hour to figure out why the function did not have permissions to run.

  • @kamil_supabase_enjoyer
    @kamil_supabase_enjoyer Рік тому +1

    That was really good. Thank you. I would like to also see how to do it in SQL editor.

  • @ernestaszemaitis5253
    @ernestaszemaitis5253 Рік тому

    I am stuck with a superbase trigger. I can't set trigger on auth.users. In Add a new Trigger > Conditions to fire trigger > Table there is only one 'profiles' table. What am i doing wrong?

    • @zb1921
      @zb1921 Рік тому

      You'll find the solution in my latest comment.

    • @maybe_omuamua
      @maybe_omuamua 11 місяців тому

      Failed to create function: failed to create pg.functions: syntax error at or near "return"

  • @haiderjaafer8164
    @haiderjaafer8164 2 роки тому +2

    Great work keep going forward…. Is supabase support mongodb also

    • @didiercatz
      @didiercatz 2 роки тому +1

      No, supabase is a database of its own.

    • @haiderjaafer8164
      @haiderjaafer8164 2 роки тому

      @@didiercatz
      There no any type of no sql database in supabase

    • @JonMeyers
      @JonMeyers 2 роки тому +2

      Supabase wraps around PostgreSQL atm, and is really just a convenience wrapper trying to expose all the cool things that Postgres give you out of the box! Therefore, I think it is unlikely Supabase would move away from Postgres. Anything is possible though!

  • @jkohlin
    @jkohlin Рік тому

    What if you want to create a trigger function on deletion of a row. How do I get a reference to the primary key from the deleted row, so that I can clean up rows from other tables based on that?

    • @Supabase
      @Supabase  Рік тому +1

      >so that I can clean up rows from other tables
      It sounds like you want "cascading deletes". When you set up a column in a table which references another table you can choose to use "cascade delete" which will delete any references
      for the trigger, you can access the ID using the "OLD" values inside the function - eg: OLD.id

    • @jkohlin
      @jkohlin Рік тому

      @@Supabase ahaa that is awesome. How convenient! Thank you very much!

    • @jkohlin
      @jkohlin Рік тому

      @@Supabase 🤔...or that would have been awesome if it was true... Sorry, but I cant find cascading deletes anywhere. When I delete a user from auth users I just get an error referring to foreign keys. But when I edit the foreign keys-column in those tables, there is no such option 😟
      Or maybe there's no UI for it yet? I fixed it by first removing the relationship in the UI and then ADD CONSTRAINT via an SQL query. Thanks anyway for pointing me in the right direction

    • @Supabase
      @Supabase  Рік тому

      @@jkohlin For now, you will need to add the column using SQL to get the CASCADE DELETE. We haven't added it to the UI yet - it's coming!

  • @albertodeagostini6143
    @albertodeagostini6143 2 роки тому

    This is incredibly useful

  • @alfonguti1
    @alfonguti1 Рік тому

    Great video due! Thx :)

  • @hermeschi
    @hermeschi Рік тому

    Thanks, it was helpful

  • @dipankarmaikap2485
    @dipankarmaikap2485 2 роки тому

    Why the update event does not trigger/work only the insert works.?

    • @misomenze
      @misomenze 2 роки тому

      Did you find a solution to your question?

    • @dipankarmaikap
      @dipankarmaikap 2 роки тому +1

      @@misomenze Yes I found the answer, actually we have to create two diffrent function. inser and update action returns two diffrent type of data.

    • @misomenze
      @misomenze 2 роки тому

      ​@@dipankarmaikap hi, thanks I'm not sure I understand. What I'm trying to do is update public. users table (my users table) when I update the user's email or phone on the auth.user table. using a trigger for update. when I set it to update supabase declines any request I make on the auth. users table (signing in or signing up a new user).

    • @dipankarmaikap
      @dipankarmaikap 2 роки тому

      @@misomenze create two function first one will use insert and second one will use update. I'm trying to add a github gist link but its getting deleted. May be reach out to me in another way I'll share you a code sample.

    • @misomenze
      @misomenze 2 роки тому

      @@dipankarmaikap hi, thanks
      I just sent you a dm on Facebook

  • @shohzodzet
    @shohzodzet Рік тому

    Thank you, it works)

  • @ProfRa6574
    @ProfRa6574 6 місяців тому

    supabase are we going to get an answer on this? like ever??

  • @lasserravn
    @lasserravn Рік тому

    I got it working, but only when my profiles table has the exact same table headers as the names of the variables that are passed in the function.
    This fx. doesnt' work, but if I change the 2nd line to use full_name & avatar_url (and also rename the columns in the profiles table) then it works:
    begin
    insert into public.profiles (id, email, fullName, avatarUrl)
    values (new.id, new.raw_user_meta_data->>'email', new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');
    return new;
    end;
    Is there any way to declare which value belongs to which column in my profiles table? We need more examples on this! It's an awesome feature.