Home > OS >  Access to function on composite types from a trigger
Access to function on composite types from a trigger

Time:01-23

I have defined several functions on composite types to act like calculated fields, something like this :

CREATE OR REPLACE FUNCTION status_affaireannulee(affaires) RETURNS BOOLEAN
AS $BODY$
  SELECT COALESCE($1.affannulee,FALSE) OR $1.constatdecarence;
$BODY$ LANGUAGE sql IMMUTABLE;

Here, affaires is the name of a table. So I can call :

SELECT a.status_affaireannulee FROM affaires a WHERE idaffaire=1234;

Now I wish to use this function from within an update trigger, like so :

CREATE OR REPLACE FUNCTION affaires_update_CaseProgress() RETURNS trigger AS
$BODY$
BEGIN
  IF NEW.status_affaireannulee THEN
    RAISE NOTICE 'affaire annulée';
  END IF;
  RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;

CREATE TRIGGER affaires_caseprogress
BEFORE INSERT OR UPDATE ON affaires
FOR EACH ROW
EXECUTE PROCEDURE affaires_update_CaseProgress();

But PostgreSQL tells me "Record « new » has no « status_affaireannulee » field" (free translation from french).

Am I doing something wrong or is this impossible? I am using PostgreSQL 11.

CodePudding user response:

You can pass the NEW parameter to the function instead of using the dot notation:

status_affaireannulee(NEW)
  •  Tags:  
  • Related