error when inserting new user trigger

i get 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;

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
Was this page helpful?