Thread: Rules

Rules

From
Darko Prenosil
Date:
Why is this wrong ?:

DROP SCHEMA test CASCADE ;
CREATE SCHEMA test;

CREATE TABLE test.parent (id serial PRIMARY KEY,opis text 
);

CREATE TABLE test.child_data (id serial PRIMARY KEY,id_parent int ,podaci text,FOREIGN KEY (id_parent) REFERENCES
test.parent(id)
);

CREATE VIEW test.child AS SELECT     p.id AS id,        p.opis AS opis,        c.id AS id_data,        c.id_parent AS
id_parent,       c.podaci AS podaci FROMtest.parent p, test.child_data c WHERE c.id_parent = p.id;
 
CREATE FUNCTION test.testfi(test.child) RETURNS bool AS '
DECLARE_NEW ALIAS FOR \$1;
BEGINRAISE NOTICE ''%'',_NEW.opis;INSERT INTO test.parent (id,opis) VALUES (_NEW.id,_NEW.opis);INSERT INTO
test.child_data(id,id_parent,podaci) VALUES 
 
(_NEW.id_data,_NEW.id,_NEW.podaci);RETURN TRUE;
END; ' 
LANGUAGE 'plpgsql';

CREATE RULE child_ins AS ON INSERT TO test.child
DO INSTEAD SELECT test.testfi(NEW);


INSERT INTO test.child(id,id_data,opis,podaci) VALUES (1,1,'Opis','podaci');


Gives:(-403)ERROR:  cannot handle whole-row reference

Can I pass NEW & OLD from rule into function in any way ?
Any suggestions ?

Regards !


Re: Rules

From
Tom Lane
Date:
Darko Prenosil <darko.prenosil@finteh.hr> writes:
> CREATE RULE child_ins AS ON INSERT TO test.child
> DO INSTEAD SELECT test.testfi(NEW);

> INSERT INTO test.child(id,id_data,opis,podaci) VALUES (1,1,'Opis','podaci');

>     (-403)ERROR:  cannot handle whole-row reference

It works in CVS tip ;-).  No chance in 7.4 or earlier, however.
You'll have to pass the components of the row as separate arguments
to the function.
        regards, tom lane