👋 , I'm trying to encrypt a field when using a tr...
# help
c
👋 , I'm trying to encrypt a field when using a trigger, but I get an error when saving, any help? This works
Copy code
create or replace function public.handle_new_user() 
returns trigger as $$
begin
  insert into public.users (id, email)
  values (new.id, new.email);
  return new;
end;
$$ language plpgsql security definer;
This doesn't work - I get this error
{ message: 'Database error saving new user', status: 500 }
Copy code
create or replace function public.handle_new_user() 
returns trigger as $$
begin
  insert into public.users (id, email)
  values (new.id, pgp_sym_encrypt(new.email, 'KEY'));
  return new;
end;
$$ language plpgsql security definer;
The only difference is
pgp_sym_encrypt(new.email, 'KEY')
j
this should be because
pgp_sym_encrypt
returns
bytea
instead of text
whereas your email column is probably in text format
dont know for sure if this will work, but u might be able to cast it to text before saving:
pgp_sym_encrypt(new.email, 'KEY')::text
c
Ahh... it still didn't work for me @User but that is a great thought. When I do insert it with SQL however, everything works as expected which is odd..
Copy code
INSERT INTO users (email)
VALUES (pgp_sym_encrypt('test@email.com', 'KEY'));