Postgresql Create ForeignKey with repeated table name in value of ref contrain key
-
09-03-2021 - |
Domanda
I am working on a database which does not contain any foreign key. When i open table I see strange format key because table of parent reference is stored in the value.
exemple
CREATE TABLE foo
(
id SERIAL PRIMARY KEY,
name text
);
CREATE TABLE bar
(
id SERIAL PRIMARY KEY,
foo_id text,
name text
);
INSERT INTO foo("name")
VALUES ('john');
INSERT INTO bar("foo_id","name")
VALUES (CONCAT('foo.', 1),'doe');
INSERT INTO bar("foo_id","name")
VALUES (CONCAT('foo.', 1),'kelly');
Can I create an FK with this type of structure? I have never seen this before
Soluzione
Can I create an FK with this type of structure?
No, you can't create a FK because the value of a FK column must match the value of the referenced PK column. But there is no value foo.1
in the table foo
.
That design is horrible. I understand you didn't create that mess, but if you do have the chance to fix it, then do it as soon as possible.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a dba.stackexchange