Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 8A2C06328D4 for ; Mon, 24 May 2010 05:51:19 -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 24653-07 for ; Mon, 24 May 2010 08:51:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sargon.lncsa.com (mail.lncsa.com [213.215.28.11]) by mail.postgresql.org (Postfix) with ESMTP id 9905163289F for ; Mon, 24 May 2010 05:51:12 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id E23F4300CA4D for ; Mon, 24 May 2010 10:51:09 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at lncsa.com Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id djfjvFilO4X0 for ; Mon, 24 May 2010 10:51:09 +0200 (CEST) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id C0BE23015E24 for ; Mon, 24 May 2010 10:51:09 +0200 (CEST) Received: from delos.apartia.fr (delos.apartia.fr [10.0.3.107]) by zenon.apartia.fr (Postfix) with ESMTP id 49A005924B247 for ; Mon, 24 May 2010 10:51:09 +0200 (CEST) Received: by delos.apartia.fr (Postfix, from userid 1000) id 10AC720283886; Mon, 24 May 2010 10:51:09 +0200 (CEST) Date: Mon, 24 May 2010 10:51:09 +0200 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: safely exchanging primary keys? Message-ID: <20100524085101.GA21249@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) 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: 201005/103 X-Sequence-Number: 34151 Hi, 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? Thanks,