rules help - Mailing list pgsql-sql

From Brook Milligan
Subject rules help
Date
Msg-id 199904271426.IAA18768@trillium.nmsu.edu
Whole thread Raw
Responses Re: [SQL] rules help
List pgsql-sql
I am trying to create a view with rules to make data entry, etc. more
convenient.  The view must be a union of two tables and the rules must
manipulate the underlying tables.  Everything is fine except for one
thing I need help on.

One table must maintain a unique list of keywords that can be
referenced by >1 row in the second table.  Inserts into the view need
to insert into the keyword list if necessary, but not if not
necessary.  If I simply create the normal insert rule, some inserts to
the view fail because the keyword is not unique.  That would be fine
if either the insert into the keyword table could be conditional on
the lack of the keyword or the entire set of rules could proceed even
if the insert into the keyword table fails.

Apparently, putting a where condition on a rule doesn't work (see
error message below).

Any ideas on how to accomplish this?

Thanks for your help.

Cheers,
Brook

===========================================================================
-- tables

drop sequence rule_table_1_id_seq;
drop table rule_table_1;
create table rule_table_1
(id        serial,name        text,
unique (name)
);

drop sequence rule_table_2_id_seq;
drop table rule_table_2;
create table rule_table_2
(id        serial,table_1_id    int4        references rule_table_1 (id),address    text
);

-- view

drop view rule_view;
create view rule_view as select a.name, b.address from rule_table_1 a, rule_table_2 b
where a.id = b.table_1_id;

-- rules

create rule rule_view_insert_1a as on insert to rule_viewwhere not exists (select id from rule_table_1 where name =
new.name)do insteadinsert into rule_table_1 (name) values (new.name);
 
create rule rule_view_insert_2 as on insert to rule_view do insteadinsert into rule_table_2 (table_1_id, address)
selectid, new.address from rule_table_1 where name = new.name;
 

-- insert

insert into rule_view (name, address) values ('Tom', 'New York');
ERROR:  ExecEvalExpr: unknown expression type 108
insert into rule_view (name, address) values ('Sue', 'Boston');
ERROR:  ExecEvalExpr: unknown expression type 108
insert into rule_view (name, address) values ('Bill', 'Chicago');
ERROR:  ExecEvalExpr: unknown expression type 108
insert into rule_view (name, address) values ('Tom', 'Boston');
ERROR:  ExecEvalExpr: unknown expression type 108
select * from rule_view;
select * from rule_table_1;
select * from rule_table_2;



pgsql-sql by date:

Previous
From: Michael J Davis
Date:
Subject: RE: [SQL] substring
Next
From: Tom Lane
Date:
Subject: Re: [SQL] Strange behavior