0
今天,我開始端口MySQL數據庫PostgreSQL和我有一個外鍵和他們的「共享」PostgreSQL的引用外鍵的多個表
一個問題,我不知道該怎麼解釋,所以這裏是創建腳本的一些僞代碼:
create table id_generator (
id serial PRIMARY KEY,
description varchar(50));
create table parent (
id REFERENCES id_generator(id),
--content);
create table child (
id REFERENCES id_generator(id),
id_parent REFERENCES parent(id),
--content);
於是我就用一個表作爲「id_generator」創建ID和給其他表的值表的引用。在MySql中,我沒有任何問題,所以我希望它也能在PostgreSQL中工作。
運行該腳本時,出現錯誤消息,我需要/unique
對錶parent
創建一個引用。所以我把它改成:
create table id_generator (
id serial PRIMARY KEY,
description varchar(50));
create table parent (
id REFERENCES id_generator(id) PRIMARY KEY,
--content);
create table child (
id REFERENCES id_generator(id),
id_parent REFERENCES parent(id),
--content);
所以服務器接受創建腳本,並且所有的設置都是爲了一些插入。
我創建第一個ID有:
insert into id_generator(description) values("parentID");
然後我要添加父:
insert into parent(id, /*content*/) values(1, /*content*/);
也按預期工作,所以我需要插入一個child
爲parent
:
insert into id_generator(description) values("childID");
insert into child(id,id_parent)values(2,1);
在這裏我得到錯誤消息「ERROR:dupli美食鍵值違反唯一約束詳細信息:按鍵(id_parent)=(1)媒體鏈接存在」
編輯:
\d child;
Tabelle ╗public.child½
Spalte | Typ | Attribute
-------------------+---------+-----------
id | integer | not null
id_parent | integer |
Indexe:
"child_pkey" PRIMARY KEY, btree (id)Fremdschlⁿssel-Constraints:
"child_id_parent_fkey" FOREIGN KEY (id_parent) REFERENCES parent(id)
"child_id_generator_fkey" FOREIGN KEY (id) REFERENCES id_generator(id)
我怎麼能解決?
顯示用'=>子表結構\ d child'和'從child' – 2013-03-04 11:30:11
已完成選擇* ; '孩子'是空的 – Zaiborg 2013-03-04 12:47:41