Reputation: 545
I am using postgresql and pgadmin and I am new to this. I have a table "Users" that has the following columns (Username, Name, Email, Phone, Discount, Password, token, serial, created_on, updated_on, points, reference)
.
I was trying to create a trigger for the table so that everytime a new insert occurs with an existing username in the reference field, the points of the used username will be incremented by 50. So there are two operations - updating the existing table, inserting the new values.
I tried creating a trigger function like this:
create or replace function points()
returns trigger as
$BODY$
BEGIN
if
new."Users"."reference" in (old."Users"."username")
then
Insert into "Users"(Username,Name,Email,Phone,Discount,Password,token,serial,created_on,updated_on,points,reference)
values(new.Username,new.Name,new.Email,new.Phone,new.Discount,new.Password,new.token,new.serial,new.created_on,new.updated_on,new.points,new.reference);
update "Users"
set old."Users"."points" = old."Users"."points" + 50
where "Users"."username" = (select "Users"."username" from "Users" where new."Users"."reference" in (old."Users"."username"));
end if;
RETURN new;
END;
$BODY$
language plpgsql;
and a trigger like this:
create trigger referece_points
after insert
on "Users"
for each row
execute procedure points();
But when I try to insert a new values in the "Users" table, I get the following error:
ERROR: missing FROM-clause entry for table "Users" LINE 1: SELECT new."Users"."reference" in (old."Users"."username") ^ Query: SELECT new."Users"."reference" in (old."Users"."username") CONTEXT: PL/pgSQL function points() line 2 at IF
Upvotes: 0
Views: 646
Reputation: 45900
New is composite variable of type that is related to table joined with trigger.
The code new."Users"."reference"
has not any sense. You should to write just new."reference"
.
Upvotes: 0