SupabaseS
Supabase•4y ago
rchrdnsh

Syntax Error on query, but don't know what to fix...

I am trying to create two tables for users and place them in one or the other table based on part of their email address.

-- inserts a row into either providers or team tables based on email address
create function public.handle_and_place_new_user()

returns trigger as $$

begin
  
  if position('@arrowgtp.com' in new.email) > 0 then
    insert into public.team (id, full_name, avatar_url, role)
    values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url', 'team');
  else
    insert into public.providers (id, full_name, avatar_url)
    values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');
  end if;
  
  return new;
end;

$$ language plpgsql security definer;

-- 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_and_place_new_user()

...and I get the syntax error:

Failed to validate sql query: syntax error at or near "insert"

...but I am new to sql and don't really know what I'm doing, so it's hard for me to figure out what is wrong...

Any advice would be very appreciated, thank you 🙂
Was this page helpful?