postgresql - 未找到外键中引用的列 <表名>_id

标签 postgresql

我将在 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/

相关文章:

postgresql - 函数在没有 STRICT 修饰符的情况下执行得更快?

postgresql - 沉默 "unknown OID 17227: failed to recognize type of '地理'。它将被视为字符串。”

javascript - 如何在Sequelize中的TIME类型的列中创建一个位置?

postgresql - 在两台不同的机器上进行 GROUP BY DISTINCT 查询的不同查询计划

java - Spring jdbc 模板中的 BadSqlGrammarException

postgresql - 哪里可以找到空间关系函数的源代码?

postgresql - Postgis:函数 st_contains 不存在

postgresql - PostgreSQL 数据中 `pg_logical` 目录的用途是什么?

python - django:应用程序 x 中的迁移 x 没有迁移类

postgresql - 在 Plone 站点上调整 PostgreSQL 的 RelStorage 和参数