Database error saving new user when using trigger w/ public.users table

See original GitHub issue

Bug report

Describe the bug

I created a public.users table, with id (uuid) and email (varchar) fields, and set up the trigger as mentioned here. This results in being unable to create a user through “Authentication > Invite New User”, and through a web app using supabase.auth.signIn({ provider: "google" })

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create new project
  2. Add public users table with id: uuid, and email: varchar
  3. Execute the following SQL
CREATE OR REPLACE FUNCTION signup_copy_to_users_table()
RETURNS TRIGGER AS $$
  BEGIN
    INSERT INTO public.users (id, email)
    VALUES(new.id, new.email);
    RETURN NEW;
  END;
$$
LANGUAGE plpgsql SECURITY DEFINER;

DROP TRIGGER IF EXISTS signup_copy on auth.users;
CREATE TRIGGER signup_copy
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE PROCEDURE signup_copy_to_users_table();
  1. At this point, you will be unable to invite a user through the Authentication UI

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

26reactions
kiwicopplecommented, Feb 4, 2021

Hi all - I just tried with a new project and didn’t have any errors here. Here were the steps:

  1. Create a public.users table:
create table users (
  id uuid references auth.users not null primary key,
  email text
);
  1. create a trigger:
create or replace function public.handle_new_user() 
returns trigger as $$
begin
  insert into public.users (id, email)
  values (new.id, new.email);
  return new;
end;
$$ language plpgsql security definer;
  1. Trigger the function on invite:
-- trigger 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();
  1. Invite a user via the UI

image

  1. See the user with email in the table:

image

Let me know if I’m missing anything

6reactions
ethicnologycommented, May 14, 2021

Hey, i’ve had the same issue and this works for me, thanks. It seems that the SQL function and trigger only works if the users table is created in pure SQL not via supabase Dashboard. Should we update the documentation to avoid any other lost adventurers like us ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Database Error Saving New User error with Postgres Function?
I have created a trigger and function that would, upon insertion of a new row in auth.users , will insert a row into...
Read more >
Managing User Data - Supabase
If you want to add a row to your public.profiles table every time a user signs up, you can use triggers. If the...
Read more >
Getting this error on the trigger : r/Supabase - Reddit
Hi team I want to insert a record in a table sitting in public schema based on the record creation in auth.users table....
Read more >
Use Supabase to Subscribe to Database Events with Postgres ...
In this video, we implement a Postgres trigger to listen to INSERT events on the auth. users table and call our create_profile_for_user function....
Read more >
sql server - Trigger to restrict users from creating a new table ...
If you really must use a trigger you can use a database trigger. CREATE TRIGGER NoCreateTable ON DATABASE FOR CREATE_TABLE AS PRINT 'You...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found