X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1445F52A5E for ; Wed, 29 Jun 2005 09:53:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 51769-02 for ; Wed, 29 Jun 2005 12:53:18 +0000 (GMT) Received: from mx-00.sil.at (mx-00.sil.at [62.116.68.196]) by svr1.postgresql.org (Postfix) with ESMTP id 8608C52A51 for ; Wed, 29 Jun 2005 09:53:17 -0300 (ADT) Received: (qmail-ldap/ctrl 11963 invoked from network); 29 Jun 2005 12:53:18 -0000 Received: from unknown (HELO io.sonorys.at) ([213.235.243.42]) (envelope-sender ) by mx-00.sil.at (qmail-ldap-1.03) with SMTP for ; 29 Jun 2005 12:53:18 -0000 Received: from neptun.sonorys.at (neptun.sonorys.at [172.24.1.17]) by io.sonorys.at (8.11.7+Sun/8.11.7) with ESMTP id j5TCrKG22198 for ; Wed, 29 Jun 2005 14:53:20 +0200 (MEST) Received: by neptun.sonorys.at with Internet Mail Service (5.5.2653.19) id ; Wed, 29 Jun 2005 14:53:20 +0200 Message-ID: From: =?iso-8859-1?Q?K=D6PFERL_Robert?= To: pgsql-sql@postgresql.org Subject: Insert rule and default values for PK Date: Wed, 29 Jun 2005 14:53:20 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="iso-8859-1" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.109 tagged_above=0 required=5 tests=AWL, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200506/354 X-Sequence-Number: 21955 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.