safely exchanging primary keys? - Mailing list pgsql-sql

From Louis-David Mitterrand
Subject safely exchanging primary keys?
Date
Msg-id 20100524085101.GA21249@apartia.fr
Whole thread Raw
Responses Re: safely exchanging primary keys?  (Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>)
Re: safely exchanging primary keys?  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: safely exchanging primary keys?  (jr <jorg.raskowski@tailorware.org.uk>)
List pgsql-sql
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=id2where 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,


pgsql-sql by date:

Previous
From: Torsten Lange
Date:
Subject: Re: Access Update error: solution.
Next
From: Louis-David Mitterrand
Date:
Subject: Re: safely exchanging primary keys?