From: | Victor Sterpu <victor(at)caido(dot)ro> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org |
Subject: | Trigger triggered from a foreign key |
Date: | 2012-10-19 18:14:48 |
Message-ID: | 50819898.9090508@caido.ro |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
I have this trigger that works fine. The trigger prevents the deletion
of the last record.
But I want skip this trigger execution when the delete is done from a
external key.
How can I do this?
This is the fk
ALTER TABLE focgdepartment
ADD CONSTRAINT fk_focgdep_idfocg FOREIGN KEY (idfocg)
REFERENCES focg (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE;
This is the trigger
CREATE FUNCTION check_focgdepartment_delete_restricted() RETURNS trigger
AS $check_focgdepartment_delete_restricted$
BEGIN
IF ( (SELECT count(*) FROM focgdepartment WHERE idfocg =
OLD.idfocg)=1)
THEN RAISE EXCEPTION 'Last record can not be deleted';
END IF;
RETURN OLD;
END;
$check_focgdepartment_delete_restricted$
LANGUAGE plpgsql;
CREATE TRIGGER focgdepartment_delete_restricted BEFORE DELETE ON
focgdepartment FOR EACH ROW EXECUTE PROCEDURE
check_focgdepartment_delete_restricted();
Thank you
From | Date | Subject | |
---|---|---|---|
Next Message | David Johnston | 2012-10-19 18:24:41 | Re: Trigger triggered from a foreign key |
Previous Message | Liam Caffrey | 2012-10-19 15:46:51 | Re: Recursive temporal query |