[SOLVED] Supabase triggers : Getting syntax error at or near “select” (SQL)

Issue

This Content is from Stack Overflow. Question asked by Abhishek MG

Iam trying to insert a new row in profiles table when a new user is created in auth table using a trigger in the supabase dashboard

But when creating the function for the trigger I get Failed to create function: failed to update pg.functions with the given ID: syntax error at or near "select" error

This is the function that is called in the trigger

begin
  insert into public.profiles (id,email,user_name)
  values (new.id, new.email, select left(replace(new.email, '.', '-'), charindex('@', replace(new.email, '.', '-')) - 1));
  return new;
end;

The profiles table has 3 columns , id,email,user_name

the user_name needs to be characters before “@” in an email , and any “.” needs to be replaced with “-” , example : "test.123@test.com" -> "test-123"

Screen shot for the created function in supabase

Iam new to SQL and having trouble figuring this out , any help is must appreciated !



Solution

Try phrasing using an INSERT INTO ... SELECT:

begin
    insert into public.profiles (id, email, user_name)
    select new.id, new.email,
           left(replace(new.email, '.', '-'), charindex('@', replace(new.email, '.', '-')) - 1));
return new;
end;


This Question was asked in StackOverflow by Abhishek MG and Answered by Tim Biegeleisen It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?