Foreign Key Fails on Trigger

I'm having an issue when I create a new user, I want to insert a row (using a trigger) into the decks table, which has a foreign key constraint of
user_id
.

I get this error: insert or update on table "decks" violates foreign key constraint "decks_user_id_fkey"

However, as seen in the snippet below, I believe I am passing this value in as new.id to the field
user_id


Is there a way I can log the value of new.id? Or am I doing something obviously incorrect here?

What I've tried: I tested the insert in isolation using hardcoded data, tested the function / trigger with mock examples. It just seems to be when I try to pass the new.id to real data with real constraints that it fails.

create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
DECLARE
  deckId uuid := gen_random_uuid();
begin
  insert into public.decks(id, description, title, user_id)
  values (deckId, 'test', 'test', new.id);
  return new;
end;
$$;

create trigger on_user_created
  after insert on public.cards
  for each row execute procedure public.handle_new_user();
Was this page helpful?