© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•3y ago•
24 replies
.Volxen

error when inserting new user trigger

i get
insert or update on table \"users\" violates foreign key constraint \"users_id_fkey\"
insert or update on table \"users\" violates foreign key constraint \"users_id_fkey\"
error when signing up and my trigger inserting a new row for the user, the function looks like this:
begin
  if exists(select 1 from public.users where nickname = trim(both '"' from (new.raw_user_meta_data -> 'nickname')::text))
  then
    raise exception 'Nickname already exists in users table';
  end if;
  
  insert into public.users (id, nickname, avatar)
  values (new.id, CASE
    WHEN new.raw_user_meta_data -> 'nickname' IS NOT NULL THEN trim(both '"' from (new.raw_user_meta_data -> 'nickname')::text)
    ELSE split_part(new.email,'@',1)
  END, 'https://api.dicebear.com/5.x/micah/svg?seed=' ||CASE
    WHEN new.raw_user_meta_data -> 'nickname' IS NOT NULL THEN trim(both '"' from (new.raw_user_meta_data -> 'nickname')::text)
  END || '&radius=50');
  
  return new;
end;
begin
  if exists(select 1 from public.users where nickname = trim(both '"' from (new.raw_user_meta_data -> 'nickname')::text))
  then
    raise exception 'Nickname already exists in users table';
  end if;
  
  insert into public.users (id, nickname, avatar)
  values (new.id, CASE
    WHEN new.raw_user_meta_data -> 'nickname' IS NOT NULL THEN trim(both '"' from (new.raw_user_meta_data -> 'nickname')::text)
    ELSE split_part(new.email,'@',1)
  END, 'https://api.dicebear.com/5.x/micah/svg?seed=' ||CASE
    WHEN new.raw_user_meta_data -> 'nickname' IS NOT NULL THEN trim(both '"' from (new.raw_user_meta_data -> 'nickname')::text)
  END || '&radius=50');
  
  return new;
end;

it worked fine before but weirdly doesnt now, am assuming the new.id is correct way of saving the users id but maybe i should use auth.uid() ? or does that not exist yet
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Database error saving new user when using trigger
SupabaseSSupabase / help-and-questions
3y ago
How to read user_metadata inside trigger function when new user is created?
SupabaseSSupabase / help-and-questions
4y ago
error saving new user?
SupabaseSSupabase / help-and-questions
4y ago
Unknown DB Error when creating a new user
SupabaseSSupabase / help-and-questions
13mo ago