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