Search Postgresql Archives

Foreign key reference causes invalid DELETE trigger calls

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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. 



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux