duplicate key value violates unique constraint "profiles_pkey"

I get this error trying to sign up the user using the premade supabase ThemeSupa auth ui. I previously had a user but changed my functions and database tables after I authenticated with that user. I thought deleting the user would fix the problem but the error still appeared.

This is my database query
create table profiles (
  id uuid references auth.users on delete cascade not null primary key,
  updated_at timestamp with time zone,
  full_name text null,
  age bigint null,
  bio text null,
  avatar_url text
);

-- This trigger automatically creates a profile entry when a new user signs up via Supabase Auth.
-- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details.
create function public.handle_new_user()
returns trigger as $$
begin
  insert into public.profiles (id, full_name, age, bio, avatar_url)
  values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'age', new.raw_user_meta_data->>'bio', new.raw_user_meta_data->>'avatar_url');
  return new;
end;
$$ language plpgsql security definer;
create trigger on_auth_user_created_new
  after insert on auth.users
  for each row execute procedure public.handle_new_user();


I have RLS policies but omitted them

This is the error message:
 "error": "failed to close prepared statement: ERROR: current transaction is aborted, commands ignored until end of transaction block (SQLSTATE 25P02): ERROR: duplicate key value violates unique constraint \"profiles_pkey\" (SQLSTATE 23505)"
Was this page helpful?