Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 9D2391337BB4 for ; Thu, 5 May 2011 12:38:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 52742-05 for ; Thu, 5 May 2011 15:38:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from zebedee.cranntara.net (zebedee.cranntara.net [91.186.7.79]) by mail.postgresql.org (Postfix) with ESMTP id 8CFFE1337BEF for ; Thu, 5 May 2011 12:38:04 -0300 (ADT) Received: from [88.210.188.180] (helo=bolek.coachhouse) by zebedee.cranntara.net with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1QI0cj-0001wT-PZ for pgsql-sql@postgresql.org; Thu, 05 May 2011 16:38:02 +0100 Received: from dick.coachhouse ([192.168.1.32] ident=tarlika) by bolek.coachhouse with esmtp (Exim 4.69) (envelope-from ) id 1QI0NE-0006GC-Nm for pgsql-sql@postgresql.org; Thu, 05 May 2011 16:21:52 +0100 Date: Thu, 5 May 2011 16:21:38 +0100 From: Tarlika Elisabeth Schmitz To: pgsql-sql@postgresql.org Subject: Re: data import via COPY, Rules + Triggers Message-ID: <20110505162138.0981c143@dick.coachhouse> In-Reply-To: References: <20110504234804.0d0a1fff@dick.coachhouse> X-Mailer: Claws Mail 3.5.0 (GTK+ 2.12.12; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201105/22 X-Sequence-Number: 35306 Thank you for your help, Sergey. That certainly works. I was wondering whether the manager.id could maybe be obtained via INSERT ... RETURNING? -- Best Regards, Tarlika Elisabeth Schmitz On Thu, 5 May 2011 08:45:32 +0300 sergey kapustin wrote: >Try using (select id from manager where name=NEW.manager_name) to get >the newly inserted manager. >The "name" column in "manager" table should have unique constraint - >this will be good both for performance and consistency. > > > >CREATE OR REPLACE RULE zathlete_insert_1 AS > ON INSERT TO zathlete > DO ALSO > ( > INSERT INTO athlete > (id, name, _received) VALUES > (NEW.dad_id, NEW.dad_name, NEW._received); > INSERT INTO sponsor > (id, name, _received) VALUES > (NEW.sponsor_id, NEW.sponsor_name, NEW._received); > INSERT INTO manager > (name, _received) VALUES > (NEW.manager_name, NEW._received); > INSERT INTO athlete > (id, name, dad_fk, sponsor_fk, manager_fk, _received) VALUES > (NEW.id, NEW.name, NEW.dad_id, > NEW.sponsor_id, (select id from manager where >name=NEW.manager_name), NEW._received); >) >; > >On Thu, May 5, 2011 at 1:48 AM, Tarlika Elisabeth Schmitz < >postgresql3@numerixtechnology.de> wrote: > >> [...] >> >> >> I created interim tables matching the structure of the CSV formats >> (about 6 of them). I want to import via COPY and distribute the data >> to the "proper" tables via rules + triggers. >> >> I just hit a wall with one of the rules, (see example below): how do >> I populate athlete.manager_fk, which is the result of the previous >> INSERT? >> >> >> >> >> >> -- interim table >> CREATE TABLE zathlete >> ( >> id integer NOT NULL, >> "name" character varying(50) NOT NULL, >> dad_id integer, >> dad_name character varying(50), >> sponsor_id integer, >> sponsor_name character varying(50), >> manager_name character varying(50), >> _received timestamp without time zone NOT NULL >> ) >> >> -- proper tables >> CREATE TABLE sponsor >> ( >> id integer NOT NULL, >> "name" character varying(50) NOT NULL, >> _received timestamp without time zone NOT NULL, >> CONSTRAINT sponsor_pkey PRIMARY KEY (id) >> ) >> >> CREATE TABLE manager >> ( >> id serial NOT NULL, >> "name" character varying(50) NOT NULL, >> _received timestamp without time zone NOT NULL, >> CONSTRAINT manager_pkey PRIMARY KEY (id) >> ) >> >> CREATE TABLE athlete >> ( >> id integer NOT NULL, >> "name" character varying(50) NOT NULL, >> dad_fk integer, >> sponsor_fk integer, >> manager_fk integer, >> _received timestamp without time zone NOT NULL, >> CONSTRAINT athlete_pkey PRIMARY KEY (id), >> CONSTRAINT manager_athlete_fk FOREIGN KEY (manager_fk) >> REFERENCES manager (id) MATCH SIMPLE >> ON UPDATE CASCADE ON DELETE RESTRICT, >> CONSTRAINT sponsor_athlete_fk FOREIGN KEY (sponsor_fk) >> REFERENCES sponsor (id) MATCH SIMPLE >> ON UPDATE CASCADE ON DELETE RESTRICT, >> CONSTRAINT dad_athlete_fk FOREIGN KEY (dad_fk) >> REFERENCES athlete (id) MATCH SIMPLE >> ON UPDATE CASCADE ON DELETE RESTRICT >> ) >> >> >> -- rules >> >> CREATE OR REPLACE RULE zathlete_insert_1 AS >> ON INSERT TO zathlete >> DO ALSO -- INSTEAD once all is working >> ( >> INSERT INTO athlete >> (id, name, _received) VALUES >> (NEW.dad_id, NEW.dad_name, NEW._received); >> INSERT INTO sponsor >> (id, name, _received) VALUES >> (NEW.sponsor_id, NEW.sponsor_name, NEW._received); >> INSERT INTO manager >> (name, _received) VALUES >> (NEW.manager_name, NEW._received); >> INSERT INTO athlete >> (id, name, dad_fk, sponsor_fk, manager_fk, _received) >> VALUES (NEW.id, NEW.name, NEW.dad_id, >> NEW.sponsor_id, ?????, NEW._received); >> ) >> >> >> >> >> >> ==== >> System: PostgreSQL 8.3 >> no of users: 1 >> >> -- >> >> Best Regards, >> Tarlika Elisabeth Schmitz