Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 1F5022E367A for ; Thu, 24 Jan 2008 05:47:36 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 72775-04 for ; Thu, 24 Jan 2008 05:47:31 -0400 (AST) Received: from mail.attiksystem.ch (f29.attiksystem.ch [212.147.59.29]) by postgresql.org (Postfix) with ESMTP id 52ABF2E36EC for ; Thu, 24 Jan 2008 05:42:30 -0400 (AST) Received: from poweredge.attiksystem.ch (poweredge.attiksystem.ch [10.0.0.29]) by mail.attiksystem.ch (8.12.11/8.12.11) with ESMTP id m0O9gQg1074873 for ; Thu, 24 Jan 2008 10:42:27 +0100 (CET) (envelope-from philippe.lang@attiksystem.ch) Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: SQL tree duplication X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Date: Thu, 24 Jan 2008 10:42:26 +0100 Message-ID: <6C0CF58A187DA5479245E0830AF84F4218D00A@poweredge.attiksystem.ch> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: SQL tree duplication thread-index: AchebW8Gw1MT1sn5SCOLZbF25W9lHg== From: "Philippe Lang" To: X-Scanned-By: MIMEDefang 2.49 on 10.0.0.111 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/180 X-Sequence-Number: 30033 Hi, I have the following kind of sql structure, in 3 levels: ----------------------- create table documents ( id serial, name varchar(50), primary key (id) ); create table lines ( id serial, name varchar(50), document_id integer, primary key (id), foreign key (document_id) references documents (id) ); create table line_details ( id serial, name varchar(50), line_id integer, primary key (id), foreign key (line_id) references lines (id) ); ----------------------- I'd like to be able to "duplicate" a document, with all of its lines and line details. Is there any easy way to do that with Postgresql? The only solution I can think of at the moment is to loop through all lines and line details, and replace foreign keys properly with values fetch with "currval". It should work just fine, but I was wondering if some advanced features of Postgresql could help in this situation. Regards, Philippe Lang