© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
2 replies
FunHellion

Same trigger for insert and delete

Hi there,

I was working on some trigger for updating the claims in the JWT for the user that gets changes to their permissions.
The following function came to life because it was not updating on deletes but only on inserts.

create or replace function update_claims_member_schemas()
returns trigger as
$$
  declare
    nothing text;
  begin
    select update_user_claims(new.user_id) into nothing;
    select update_user_claims(old.user_id) into nothing;
    return new;
  end;
$$
language plpgsql security definer;

create trigger user_roles_update_user_claims after delete or insert on user_roles
for each row execute
  procedure update_claims_member_schemas(user_id);
create or replace function update_claims_member_schemas()
returns trigger as
$$
  declare
    nothing text;
  begin
    select update_user_claims(new.user_id) into nothing;
    select update_user_claims(old.user_id) into nothing;
    return new;
  end;
$$
language plpgsql security definer;

create trigger user_roles_update_user_claims after delete or insert on user_roles
for each row execute
  procedure update_claims_member_schemas(user_id);

I have to execute the function for new and old now because an insert gives an object called
new
new
and a delete gives an object called
old
old
.

Because this seems incorrect I was wondering if there is a better way to do this. Is there an object which is always available or is this the correct way?


Thanks in advance!
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Impersonation possible for insert, update and delete operations?
SupabaseSSupabase / help-and-questions
3y ago
Trigger on Queue insert
SupabaseSSupabase / help-and-questions
5mo ago
Trigger storage delete on row delete
SupabaseSSupabase / help-and-questions
3y ago
Trigger edge function on insert
SupabaseSSupabase / help-and-questions
4y ago