Unexpected error in database trigger

Following the basic example of creating a profile when a new user is created in the auth.users table, it works fine when simply inserting a new record into the public.profiles table as follows:
begin
  insert into public.profiles (id, email, invite_id)
  values (new.id, new.email, (new.raw_user_meta_data ->> 'invite_id')::uuid);
  return new;
end;


However, if I use the same invite_id from raw_user_meta_data to update the public.invites table as follows, I get an error:
begin
  update public.invites set receiver_id = new.id
  where id = (new.raw_user_meta_data ->> 'invite_id')::uuid;

  insert into public.profiles (id, email)
  values (new.id, new.email);
  return new;
end;


The error has a code of "unexpected_failure" and a message of "Database error saving new user". The schema is correct and the RLS policy allows updates to the public.invites table, not sure what else it could be. Hard to debug these triggers.
Was this page helpful?