【发布时间】:2021-06-09 10:55:47
【问题描述】:
我使用的是 PostgreSQL 版本 13.2,我需要插入一个外部表(Postgres 也是),它有一个序列生成的 id,从外部表返回生成的 id。
我添加了“postgres_fdw”扩展,创建了服务器和用户映射。
当我这样做时:
INSERT INTO public.remote_users (name) VALUES ('username') RETURNING id;
使用以下外部表定义:
CREATE FOREIGN TABLE public.remote_users
(
id bigint,
name character varying(20)
)
SERVER remote
OPTIONS (schema_name 'public', table_name 'users');
我收到一条错误消息,指出 id 不能为空(因为 fdw 使用 'id' 列构建远程插入语句并且它具有非空约束)。
错误:关系“用户”的“id”列中的空值违反非空约束 详细信息:失败行包含(空,用户名)。 上下文:远程 SQL 命令:INSERT INTO public.users(id, name) VALUES ($1, $2) RETURNING id SQL状态:23502
使用这个外部表定义:
CREATE FOREIGN TABLE public.remote_users
(
name character varying(20)
)
SERVER remote
OPTIONS (schema_name 'public', table_name 'users');
我收到一条错误消息,提示“列“id”不存在”。
有什么方法可以使INSERT INTO public.remote_users (name) VALUES ('username') RETURNING id; 语句起作用?
【问题讨论】:
-
我认为您必须在外部表定义中包含 ID
标签: postgresql postgres-fdw sql-returning