![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I have this function which swaps primary keys for cabin_types (so that id_cabin_type ordering reflects natural data ordering): |
#3
| |||
| |||
|
|
When I run that function it seems the foreign keys are not properly updated and the data ends up in a mess. |
#4
| |||
| |||
|
|
On Mon, May 24, 2010 at 10:51:01AM +0200, Louis-David Mitterrand wrote: Hi, I have this function which swaps primary keys for cabin_types (so that id_cabin_type ordering reflects natural data ordering): Actually this function works fine. My problem was elsewhere. Sorry for barking up the wrong tree. |
#5
| |||
| |||
|
|
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? |
|
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); |
#6
| |||
| |||
|
|
Louis-David Mitterrand <vindex+lists-pgsql-sql (AT) apartia (DOT) org> wrote: 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); |
#7
| |||
| |||
|
|
On Mon, May 24, 2010 at 02:38:39PM +0000, Tim Landscheidt wrote: 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); Nice, thanks. |
#8
| |||
| |||
|
|
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); Nice, thanks. Ah, but this won't work as the UNIQUE PK constraint is in force. |
![]() |
| Thread Tools | |
| Display Modes | |
| |