From: | Andreas Kretschmer <akretschmer(at)spamfence(dot)net> |
---|---|
To: | pgsql-de-allgemein(at)postgresql(dot)org |
Subject: | Re: FOREIGN KEY |
Date: | 2009-01-25 20:49:37 |
Message-ID: | 20090125204937.GA16011@tux |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-de-allgemein |
Olaf Radicke <briefkasten(at)olaf-radicke(dot)de> schrieb:
> Hi!
>
> Ich habe eine recht komplexe Tabellenstruktur. Um die Konsistenz sicher zu
> stellen, habe ich exzessiven Gebrauch von FOREIGN KEY gemacht. Jetzt kam ein
> Refactoring der DB und dutzende Tabellen und Spalten wurden umbenannt.
> Seid dem ist die Konsistenz nicht mehr durch FOREIGN KEY geschützt. Ich weiß
> nicht ob tatsächlich das eine mit dem anderen zu tun hat. Ist nur eine
> Vermutung von mir. CONSTRAINTs sind noch da, aber ich kann wild alles löschen
> ohne das mir die DB einhalt gebietet. Also: Wenn Tabellen und Spalten
> umbenannt werden, gehen dabei dann die Verknüpfungen der FOREIGN KEY
> verloren?
Welche Version? Und warum prüfst Du es nicht?
test=# create table t1 (id int primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey"
for table "t1"
CREATE TABLE
Zeit: 6,651 ms
test=*# create table t2 (id int references t1);
CREATE TABLE
Zeit: 4,305 ms
test=*# alter table t1 rename column id to id_new;
ALTER TABLE
Zeit: 0,406 ms
test=*# \d t2
Tabelle »public.t2«
Spalte | Typ | Attribute
--------+---------+-----------
id | integer |
Fremdschlüssel-Constraints:
»t2_id_fkey« FOREIGN KEY (id) REFERENCES t1(id_new)
Und nun mal schauen, was passiert:
test=*# insert into t2 values (1);
ERROR: insert or update on table "t2" violates foreign key constraint
"t2_id_fkey"
DETAIL: Key (id)=(1) is not present in table "t1".
Okey, und nun mit RENAME der Tabelle:
test=*# rollback;
ROLLBACK
Zeit: 0,680 ms
test=# create table t1 (id int primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey"
for table "t1"
CREATE TABLE
Zeit: 11,563 ms
test=*# create table t2 (id int references t1);
CREATE TABLE
Zeit: 1,614 ms
test=*# alter table t1 rename to new_t1;
ALTER TABLE
Zeit: 19,189 ms
test=*# \d t2
Tabelle »public.t2«
Spalte | Typ | Attribute
--------+---------+-----------
id | integer |
Fremdschlüssel-Constraints:
»t2_id_fkey« FOREIGN KEY (id) REFERENCES new_t1(id)
test=*# insert into t2 values (1);
ERROR: insert or update on table "t2" violates foreign key constraint
"t2_id_fkey"
DETAIL: Key (id)=(1) is not present in table "new_t1".
test=!#
Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
From | Date | Subject | |
---|---|---|---|
Next Message | Olaf Radicke | 2009-01-25 23:20:53 | Re: FOREIGN KEY |
Previous Message | Olaf Radicke | 2009-01-25 19:47:32 | FOREIGN KEY |