rules help

From: Brook Milligan <brook(at)trillium(dot)NMSU(dot)Edu>
To: pgsql-sql(at)postgresql(dot)org
Subject: rules help
Date: 1999-04-27 14:26:08
Message-ID: 199904271426.IAA18768@trillium.nmsu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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_view
where not exists (select id from rule_table_1 where name = new.name) do instead
insert into rule_table_1 (name) values (new.name);
create rule rule_view_insert_2 as on insert to rule_view do instead
insert into rule_table_2 (table_1_id, address) select id, 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;

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 1999-04-27 14:31:01 Re: [SQL] Strange behavior
Previous Message Michael J Davis 1999-04-27 14:06:43 RE: [SQL] substring