Postgres:外表的外键 [英] Postgres: foreign key to foreign table

查看:140
本文介绍了Postgres:外表的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张外国桌子,例如:

I have a foreign table, for example:

CREATE FOREIGN TABLE film (
    id          varchar(40) NOT NULL,
    title       varchar(40) NOT NULL,
    did         integer NOT NULL,
    date_prod   date,
    kind        varchar(10),
    len         interval hour to minute
)
SERVER film_server;

,其中id为该表的主键(在远程数据库中设置)。我想让一个本地表引用该外表,并在该本地表上设置外键约束-例如:

with id as the primary key for that table (set in the remote database). I would like to have a local table reference the foreign table, and set a foreign key constraint on the local table -- for example:

CREATE TABLE actor (
    id          varchar(40) NOT NULL,
    name       varchar(40) NOT NULL,
    film_id       varchar(40) NOT NULL,
)

ALTER TABLE actor ADD CONSTRAINT actor_film_fkey FOREIGN KEY (film_id) 
    REFERENCES film(id);

但是,当我尝试添加外键约束时,出现错误:

However, when I try to add the foreign key constraint, I get the error:

ERROR:  referenced relation "film" is not a table

是否可以向外表添加外键约束?

Is it possible to add a foreign key constraint to a foreign table?

推荐答案

无法在外部表上创建索引。

It's no possible create index on foreign tables.

创建索引idx_film ON电影(id);

CREATE INDEX idx_film ON film (id);

这是错误:

错误:无法在外部表上创建索引

ERROR: cannot create index on foreign table

这篇关于Postgres:外表的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆