Foreign key reference causes invalid DELETE trigger calls

From: "Andrus" <eetasoft(at)online(dot)ee>
To: pgsql-general(at)postgresql(dot)org
Subject: Foreign key reference causes invalid DELETE trigger calls
Date: 2005-11-09 18:43:28
Message-ID: dktg0t$tcu$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I need to log table "foo" real deletes.

"foo" has foreign key relation established but no data exists.

Postgres 8.1 calls "foo" delete trigger when record is deleted from master
table "klient". Why ?

How to modify the following code so that record is inserted into serveri
table only when records are really deleted from foo table ?
Is it possible to add some check into trigger code?

CREATE TABLE serverti ( notice char(50));

CREATE FUNCTION setlastchange() RETURNS "trigger" AS $$
BEGIN
INSERT INTO serverti values ('changed');
RETURN NULL;
END$$ LANGUAGE plpgsql;

CREATE table klient ( kood integer primary key );

CREATE TABLE foo (
klient char(12) NOT NULL,
toode char(20) NOT NULL,
CONSTRAINT foo_pkey PRIMARY KEY (klient, toode),
CONSTRAINT foo_klient_fkey FOREIGN KEY (klient)
REFERENCES klient (kood) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE
) ;

CREATE TRIGGER foo_trig
BEFORE INSERT OR UPDATE OR DELETE ON foo
FOR EACH STATEMENT
EXECUTE PROCEDURE setlastchange();

insert into klient values (1);
-- Next line causes execution of foo_trig. Why ?
delete from klient where kood=1;

Andrus.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-11-09 18:44:36 Re: Where Statement
Previous Message Mikael Carneholm 2005-11-09 18:38:53 Hanging creating of function