pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Brook Milligan <brook@trillium.NMSU.Edu>
To: pgsql-sql@postgresql.org
Subject: rules help
Date: Tue, 27 Apr 1999 08:26:08 -0600 (MDT)
Message-ID: <199904271426.IAA18768@trillium.nmsu.edu> (raw)

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;




view thread (3+ messages)  latest in thread

Message-ID: <199904271426.IAA18768@trillium.nmsu.edu>
Permalink:  ../199904271426.IAA18768@trillium.nmsu.edu/
Also on:    postgresql.org/message-id/199904271426.IAA18768@trillium.nmsu.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: brook@trillium.NMSU.Edu
  Subject: Re: rules help
  In-Reply-To: <199904271426.IAA18768@trillium.nmsu.edu>
  References: <199904271426.IAA18768@trillium.nmsu.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox