Postgres:创建外键关系,得到 'Key is not present in table'?
Postgres: create foreign key relationship, getting 'Key is not present in table'?
我在 Postgres 9.1 中工作,我想为目前没有的两个 table 创建一个外键关系。
这些是我的 table:
# \d frontend_item;
Table "public.frontend_item"
Column | Type | Modifiers
-------------------+-------------------------+--------------------------------------------------------------------
id | integer | not null default nextval('frontend_prescription_id_seq'::regclass)
presentation_code | character varying(15) | not null
pct_code | character varying(3) | not null
Indexes:
"frontend_item_pkey" PRIMARY KEY, btree (id)
# \d frontend_pct;
Column | Type | Modifiers
------------+--------------------------+-----------
code | character varying(3) | not null
Indexes:
"frontend_pct_pkey" PRIMARY KEY, btree (code)
"frontend_pct_code_1df55e2c36c298b2_like" btree (code varchar_pattern_ops)
这就是我正在尝试的:
# ALTER TABLE frontend_item ADD CONSTRAINT pct_fk
FOREIGN KEY (pct_code) REFERENCES frontend_pct(code) ON DELETE CASCADE;
但是我得到这个错误:
ERROR: insert or update on table "frontend_item" violates
foreign key constraint "pct_fk"
DETAIL: Key (pct_code)=(5HQ) is not present in table "frontend_pct"
我想这是有道理的,因为目前 frontend_pct
table 是空的,而 frontend_item
中有值。
首先,我的ALTER TABLE
语法正确吗?
其次,在 frontend_pct
中是否有自动创建所需值的方法?如果有某种方式可以对 Postgres 说,那就太好了 "create the foreign key, and insert values into the foreign key table if they don't exist".
你的语法似乎是正确的。
不,没有自动插入所需值的方法。
您只能在添加约束之前手动进行。在你的情况下必须是这样的
INSERT INTO frontend_pct (code)
SELECT code FROM
(
SELECT DISTINCT pct_code AS code
FROM frontend_item
WHERE pct_code NOT IN (SELECT code FROM frontend_pct)
) AS a;
注意:
如果您有大量数据,查询可能会很繁重..
我在 Postgres 9.1 中工作,我想为目前没有的两个 table 创建一个外键关系。
这些是我的 table:
# \d frontend_item;
Table "public.frontend_item"
Column | Type | Modifiers
-------------------+-------------------------+--------------------------------------------------------------------
id | integer | not null default nextval('frontend_prescription_id_seq'::regclass)
presentation_code | character varying(15) | not null
pct_code | character varying(3) | not null
Indexes:
"frontend_item_pkey" PRIMARY KEY, btree (id)
# \d frontend_pct;
Column | Type | Modifiers
------------+--------------------------+-----------
code | character varying(3) | not null
Indexes:
"frontend_pct_pkey" PRIMARY KEY, btree (code)
"frontend_pct_code_1df55e2c36c298b2_like" btree (code varchar_pattern_ops)
这就是我正在尝试的:
# ALTER TABLE frontend_item ADD CONSTRAINT pct_fk
FOREIGN KEY (pct_code) REFERENCES frontend_pct(code) ON DELETE CASCADE;
但是我得到这个错误:
ERROR: insert or update on table "frontend_item" violates
foreign key constraint "pct_fk"
DETAIL: Key (pct_code)=(5HQ) is not present in table "frontend_pct"
我想这是有道理的,因为目前 frontend_pct
table 是空的,而 frontend_item
中有值。
首先,我的ALTER TABLE
语法正确吗?
其次,在 frontend_pct
中是否有自动创建所需值的方法?如果有某种方式可以对 Postgres 说,那就太好了 "create the foreign key, and insert values into the foreign key table if they don't exist".
你的语法似乎是正确的。
不,没有自动插入所需值的方法。
您只能在添加约束之前手动进行。在你的情况下必须是这样的
INSERT INTO frontend_pct (code)
SELECT code FROM
(
SELECT DISTINCT pct_code AS code
FROM frontend_item
WHERE pct_code NOT IN (SELECT code FROM frontend_pct)
) AS a;
注意:
如果您有大量数据,查询可能会很繁重..