Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C6DB2633B7E for ; Mon, 24 May 2010 11:39:02 -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 82606-05 for ; Mon, 24 May 2010 14:38:50 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 86A446327E1 for ; Mon, 24 May 2010 11:38:51 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OGYnp-0001C2-3f for pgsql-sql@postgresql.org; Mon, 24 May 2010 16:38:49 +0200 Received: from g224121215.adsl.alicedsl.de ([92.224.121.215]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 24 May 2010 16:38:49 +0200 Received: from tim by g224121215.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 24 May 2010 16:38:49 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org connect(): No such file or directory From: Tim Landscheidt Subject: Re: safely exchanging primary keys? Date: Mon, 24 May 2010 14:38:39 +0000 Organization: Lines: 41 Message-ID: References: <20100524085101.GA21249@apartia.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: g224121215.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:OH+IP+T1ops3IL1U3nV1MD0K65U= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201005/107 X-Sequence-Number: 34155 Louis-David Mitterrand wrote: > I have this function which swaps primary keys for cabin_types (so that > id_cabin_type ordering reflects natural data ordering): > CREATE OR REPLACE FUNCTION swap_cabin_types(id1 integer, id2 integer) RETURNS integer > AS $$ > declare > tmp integer; > begin > tmp := nextval('cabin_type_id_cabin_type_seq'); > update cabin_type set id_cabin_type=tmp where id_cabin_type=id1; > update cabin_type set id_cabin_type=id1 where id_cabin_type=id2; > update cabin_type set id_cabin_type=id2 where id_cabin_type=tmp; > return tmp; > end; > $$ > LANGUAGE plpgsql; > 'id_cabin_type' is a foreign key for two other tables, 'cabin_category' > and 'alert_cabin_type', which have an "on update cascade" clause. > When I run that function it seems the foreign keys are not properly > updated and the data ends up in a mess. > Did I forget something? What does "are not properly updated" mean? Anyhow, why don't you use something simple like (untested): | UPDATE cabin_type | SET id_cabin_type = | CASE | WHEN id_cabin_type = id1 THEN | id2 | ELSE | id1 | END | WHERE id_cabin_type IN (id1, id2); Tim