I have user table and profile table. When a record is created in user table, it automatically create a record in profile table with the returned id. The profile table is like:
create table app_public.profiles (
user_id int not null primary key references users,
first_name text check (char_length(first_name) < 50),
last_name text check (char_length(last_name) < 50),
display_name text
);
When a user update the profile table and enter values for first_name
and last_name
, I want a trigger function to concat the two columns and use the result to update display_name
.
Currently I define the trigger function as
create function app_public.tg_user_display_name() returns trigger as $$
begin
NEW.display_name = CONCAT(profiles.first_name || ' ' || NEW.profiles.last_name);
return NEW;
end;
$$ language plpgsql volatile set search_path from current;
Also I have the trigger as
create trigger _500_update_display_name
before update on app_public.profiles
for each row
execute procedure app_public.tg_user_display_name();