Ensure user role exists #3679
-
Hi guys. The SQL User Management documentation includes the below code to check if a user role exists. Shouldn't this trigger happen before the insert or update rather than after in order to prevent the change if the role doesn't exist? create function
basic_auth.check_role_exists() returns trigger as $$
begin
if not exists (select 1 from pg_roles as r where r.rolname = new.role) then
raise foreign_key_violation using message =
'unknown database role: ' || new.role;
return null;
end if;
return new;
end
$$ language plpgsql;
create constraint trigger ensure_user_role_exists
after insert or update on basic_auth.users
for each row
execute procedure basic_auth.check_role_exists(); |
Beta Was this translation helpful? Give feedback.
Answered by
laurenceisla
Aug 8, 2024
Replies: 1 comment 1 reply
-
Hi!
If I'm not mistaken, the |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
NickEmpetvee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
If I'm not mistaken, the
RAISE
error will rollback the transaction and the insert won't be completed, even if it's in anAFTER
trigger. But still, I agree that it's more accurate to have it in aBEFORE
trigger.