i have a Postgres Trigger (After Insert) to perform a function for each row, inside of the trigger i do some subqueries, one of them involves the table “owner” of the Trigger, the problem is that the subquery is returning null value because the data isn’t in the table yet, so why this is happening if its supposed the data have to be ready (After insert trigger) when the trigger is triggered?
First i note had the trigger as Before insert, so the data wouldn’t be available because the insert wasn’t made, so the first was change before for after, i deleted the previous function and disassociate from the table then try the insert and the result is the same, null value in the subquery, thanks for your advices.(Sorry for my English)
// TRIGGER
create trigger setTimeToAg
AFTER INSERT
on archivo_gestion
for EACH ROW
EXECUTE procedure updatetime();
//TRIGGER FUNCTION
CREATE OR REPLACE FUNCTION public.updatetime()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
declare result varchar;
BEGIN
select into result from serie where
"archivoGestionId"=137;--This subquery should be
ready but not (The number is autoincrement so is
predecible)
--raise EXCEPTION 'HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAA %
%',result,new.id;
new."retentionTime"=CURRENT_DATE + (select
"tiempoAlmacenamiento" from tabla_retencion LEFT JOIN
serie on tabla_retencion.id=serie."tablaRetencionId"
where serie.id is not null and serie.id=(select id
from serie where "archivoGestionId"=new.id) and
"tipoArchivo"='AG')::NUMERIC * '1 year' :: interval;
return NEW;
end;
$function$
//INVOLVED TABLES
involved tables in the subqueries
Juaneco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.