Problem merging two rows into same primary key - Mailing list pgsql-general

From Patrik Kudo
Subject Problem merging two rows into same primary key
Date
Msg-id 4291EB4C.1080908@pingpong.net
Whole thread Raw
Responses Re: Problem merging two rows into same primary key
List pgsql-general
Hi!

I've got a problem I can't seem to find an answer to. The problem is
simplified by this example:

1. We have two tables:

create table asdf (id serial primary key,
data text);
create table qwert (id serial,
data integer references asdf
on delete cascade on update cascade);

2. We populate both tables with the following result:

keytest=# select * from asdf;
id | data
----+------
1 | asdf
2 | asd2
3 | asd3
4 | asd4
(4 rows)

keytest=# select * from qwert;
id | data
----+------
1 | 2
2 | 4
(2 rows)


Now to the problem. We want to merge rows with id = 2 and id = 4 into id
= 1 in the asdf table with the qwert table beeing updated to reflect the
change. The desired result would yeild:

keytest=# select * from asdf;
id | data
----+------
1 | asdf
3 | asd3
(2 rows)

keytest=# select * from qwert;
id | data
----+------
1 | 1
2 | 1
(2 rows)


I find no way to do this because the primary/foreign keys that would
make this easy actually makes it impossible. Are there any smart way to
do this or do I need to drop the primary key (hence also drop the
foreign keys since the drop will cascade), update the data manually and
then recreate the constraints? I hope there's an easier way beacuase in
the real scenario we're dealing with nearly 100 tables depending on that
single one with the primary key...

Thanks in advance,
Patrik Kudo

pgsql-general by date:

Previous
From: "Phil Thornhill"
Date:
Subject: Re: Postgresql .NET Data Provider for Visual Studio 2005
Next
From: Tom Lane
Date:
Subject: Re: How to recover from : "Cache lookup failed for rela tion "