Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPH5z-0004am-P7 for pgsql-sql@postgresql.org; Fri, 19 Oct 2012 18:14:55 +0000 Received: from ppp248107867.ambra.ro ([86.107.248.7] helo=caido.ro) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TPH5x-00020r-F7 for pgsql-sql@postgresql.org; Fri, 19 Oct 2012 18:14:54 +0000 Received: from localhost (localhost [127.0.0.1]) by caido.ro (Postfix) with ESMTP id 275E314199C for ; Fri, 19 Oct 2012 21:12:08 +0000 (UTC) X-Amavis-Modified: Mail body modified (using disclaimer) - caido.ro X-Virus-Scanned: amavisd-new at caido.ro Received: from caido.ro ([127.0.0.1]) by localhost (caido.ro [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id LFzRT2oV6NDj for ; Fri, 19 Oct 2012 21:12:07 +0000 (UTC) Received: from [127.0.0.1] (unknown [89.43.152.14]) by caido.ro (Postfix) with ESMTPSA id 6268314199B for ; Fri, 19 Oct 2012 21:12:07 +0000 (UTC) Message-ID: <50819898.9090508@caido.ro> Date: Fri, 19 Oct 2012 21:14:48 +0300 From: Victor Sterpu User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Trigger triggered from a foreign key Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201210/41 X-Sequence-Number: 36912 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