我将在 7 周内浏览 7 个数据库。
在 PostgreSQL 中,我创建了一个包含 SERIAL venue_id 列的 venues 表。
\d field 的输出
Table "public.venues"
Column | Type | Modifiers
----------------+------------------------+-----------------------------------------------------------
venue_id | integer | not null default nextval('venues_venue_id_seq'::regclass)
name | character varying(255) |
street_address | text |
type | character(7) | default 'public'::bpchar
postal_code | character varying(9) |
country_code | character(2) |
Indexes:
"venues_pkey" PRIMARY KEY, btree (venue_id)
Check constraints:
"venues_type_check" CHECK (type = ANY (ARRAY['public'::bpchar, 'private'::bpchar]))
Foreign-key constraints:
"venues_country_code_fkey" FOREIGN KEY (country_code, postal_code) REFERENCES cities(country_code, postal_code) MATCH FULL
下一步是创建一个使用外键引用 venue_id 的事件表。
我正在尝试这个:
CREATE TABLE events (
event_id SERIAL PRIMARY KEY,
title text,
starts timestamp,
ends timestamp,
FOREIGN KEY (venue_id) REFERENCES venues (venue_id));
我得到这个错误:
ERROR: column "venue_id" referenced in forgein key not found
怎么了?
最佳答案
您还需要初始化外键列。看这里http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-FK 来自@mu 的来源和信用太短
关于postgresql - 未找到外键中引用的列 <表名>_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176791/