pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: KÖPFERL Robert <robert.koepferl@sonorys.at>
To: pgsql-sql@postgresql.org
Subject: Insert rule and default values for PK
Date: Wed, 29 Jun 2005 14:53:20 +0200
Message-ID: <ED4E30DD9C43D5118DFB00508BBBA76EB167B0@neptun.sonorys.at> (raw)

Hi,

I've a prolem inserting records in a view using different ways.
I want to insert either way, with PK given and without PK which should then
be taken by its DEFAULT stanza:
insert into a_and_b(a) values (537)    # id not given, self assigned
insert into a_and_b(x) values (true)    # id not given, self assigned
insert into a_and_b(x,id) values (true,55)   # id given, take that

For this example I created two tables. Note the Serial at tbla:

CREATE TABLE tbla
(
 id int4 NOT NULL,
 a int4,
 b varchar(12), CONSTRAINT tbla_pk PRIMARY KEY (id)
) 

CREATE TABLE tblb
(
 id int4 NOT NULL,
 x bool, y timestamp,
 CONSTRAINT tblb_pk PRIMARY KEY (id),
 CONSTRAINT tblb_fk FOREIGN KEY (id) REFERENCES tbla (id) ON UPDATE CASCADE
ON DELETE CASCADE
) 

My view is defined as to concatinate this 1:1 relation:
CREATE OR REPLACE VIEW a_and_b AS 
 SELECT tbla.id, tbla.a, tbla.b, tblb.x, tblb.y
   FROM tbla NATURAL LEFT OUTER JOIN tblb;




In the first run I tried this insert rule. But it turned out that I can't
insert rows, if I omit id
CREATE OR REPLACE RULE a_b_insert AS
    ON INSERT TO a_and_b DO INSTEAD ( 
    INSERT INTO tbla (id, a, b) VALUES (new.id, new.a, new.b);
    INSERT INTO tblb (id, x, y) VALUES (new.id, new.x, new.y););


So I had a try with that rule which already does what was intended. I can
now choose of wheter I want to supply id of if not.
However I get disturbed by the 'nextval' and 'curval'-functions. This is
redundant with the tables definition. I don't like that.
The other way round, leave all id out, helps for tbla (since a default is
defined) but leaves tblb alone:

CREATE OR REPLACE RULE a_b_insert AS
    ON INSERT TO a_and_b DO INSTEAD ( INSERT INTO tbla (id, a, b) 
  VALUES (coalesce(new.id,nextval('public.tbla_id_seq'::text)), new.a,
new.b);
 INSERT INTO tblb (id, x, y) 
  VALUES (coalesce(new.id,currval('public.tbla_id_seq'::text)), new.x,
new.y);
);



What would you do? What is the postgres way of solving this problem.



Message-ID: <ED4E30DD9C43D5118DFB00508BBBA76EB167B0@neptun.sonorys.at>
Permalink:  ../ED4E30DD9C43D5118DFB00508BBBA76EB167B0@neptun.sonorys.at/
Also on:    postgresql.org/message-id/ED4E30DD9C43D5118DFB00508BBBA76EB167B0@neptun.sonorys.at

 · 

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: robert.koepferl@sonorys.at
  Subject: Re: Insert rule and default values for PK
  In-Reply-To: <ED4E30DD9C43D5118DFB00508BBBA76EB167B0@neptun.sonorys.at>
  References: <ED4E30DD9C43D5118DFB00508BBBA76EB167B0@neptun.sonorys.at>

* 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