SupabaseS
Supabase4y ago
John

Question about writing database functions in plpgsql.

Hi I am new to plpqsql and hope to understand functions.

I currently have a function called insert_document_contributor_by_update_documents which is triggered, as the function states, by an update in the
documents
table. This is the current version of the function.

begin
  insert into public.document_contributor(document_id, contributor)
  values(new.id, new.created_by);
  return new;
end


But I want to check if the document_contributor table has an existing record with the match of document_id and contributor. Fyi, the document_contributor table is a join table that consists of only two columns stated above, document_id and contributor, and uses both of them as a composite primary key.

How do I add a condition and modify the function to my needs?
Was this page helpful?