pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Victor Sterpu <victor@caido.ro>
To: pgsql-sql@postgresql.org
Subject: Trigger triggered from a foreign key
Date: Fri, 19 Oct 2012 21:14:48 +0300
Message-ID: <50819898.9090508@caido.ro> (raw)

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




view thread (3+ messages)  latest in thread

Message-ID: <50819898.9090508@caido.ro>
Permalink:  ../50819898.9090508@caido.ro/
Also on:    postgresql.org/message-id/50819898.9090508@caido.ro

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: victor@caido.ro
  Subject: Re: Trigger triggered from a foreign key
  In-Reply-To: <50819898.9090508@caido.ro>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox