在 postgresql 中的外表上创建外键

Bon*_*Fir 5 postgresql foreign-key postgresql-fdw

我需要我的表,另外一个是在不同的数据库(比如表链接logsdevice数据库和表accountsuser分贝,无论是同一台服务器上)。因此,使用外部数据包装器我创建了一个外部表(我通过select * from accounts;device成功运行的db 中运行来检查它)。

但是我仍然无法从devicedb创建到外表的外键

CREATE TABLE public.logs
(
  id bigint NOT NULL DEFAULT nextval('logs'::regclass),
  ...
  account_id bigint,
  ...

  CONSTRAINT logs_account_id_fkey FOREIGN KEY (account_id)
      REFERENCES public.accounts (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION

)
Run Code Online (Sandbox Code Playgroud)

错误信息是

ERROR:  referenced relation "accounts" is not a table
********** Error **********

ERROR: referenced relation "accounts" is not a table
SQL state: 42809
Run Code Online (Sandbox Code Playgroud)

ype*_*eᵀᴹ 6

您无法创建FOREIGN KEY引用不同数据库中的表或通过外部数据包装器引用表的约束。外键必须引用同一数据库中(基)表*基表:不是视图,不是外部数据包装表)。

如果您可以更改设计以将这 2 个数据库作为同一数据库中的 2 个模式,那么您可以毫无问题地创建外键。


*:还有另一个限制:永久表不能引用临时表。