Postgres 可编辑联合视图

发布于 2024-09-08 03:33:02 字数 1442 浏览 2 评论 0原文

我有一张表存储两个人之间的“链接”。为了防止我正在构建的应用程序出现进一步的复杂情况,我想创建一个可编辑视图,它显示链接记录和链接记录的反向副本。

这意味着如果 Joe 链接到 Sally,那么视图应该显示 Joe 链接到 Sally,Sally 链接到 Joe。

所以我创建了一个 UNION,如下所示:

CREATE VIEW links AS

SELECT  id,
  link_origin_id AS person_id,
  link_origin_id,
  link_rcvd_id,
  link_type,
  link_summary,
  created_at,
  updated_at
 FROM links_data 

 UNION

 SELECT  id,
   link_rcvd_id,
   link_origin_id,
   link_rcvd_id,
   link_type,
   link_summary,
   created_at,
   updated_at
 FROM links_data

视图工作正常。请注意,该视图创建了一个附加列“person_id”,该列不在基础表中。

我在创建将编辑基础表的 postgres 规则时遇到麻烦。

具体来说,当表视图不是 UNION 视图时,我可以成功编辑它。但是,当我尝试使用 UNION 视图编写规则时,会发生以下情况:

CREATE RULE inverse_links AS ON INSERT TO links DO INSTEAD
INSERT INTO links_data
  (id, link_origin_id, link_type, link_summary, link_rcvd_id, created_at, 
   updated_at)

VALUES (nextval('people_id_seq'), new.link_origin_id, new.link_type, 
        new.link_summary, new.link_rcvd_id, new.created_at, new.updated_at)

RETURNING *;

上述规则应将编辑重定向到基础表“links_data”。

但我收到以下错误:

ERROR:  RETURNING list's entry 3 has different type from column "link_origin_id"

********** Error **********

ERROR: RETURNING list's entry 3 has different type from column "link_origin_id"
SQL state: 42P17

我认为可能存在问题的两件事是 1) 视图有一个附加列,导致列类型不匹配,或者 2) 可能存在表中存在的问题本身就是一个 UNION,编辑这可能是一个问题。

知道我可以去哪里吗?

I have a table which stores 'links' between 2 people. In order prevent further complications down the road on an application I am building, I want to create an editable view, that shows the link records and an inverse copy of the link records.

Meaning if Joe is linked to Sally, then the view should show Joe linked to Sally and Sally linked to Joe.

So I have created a UNION as follows:

CREATE VIEW links AS

SELECT  id,
  link_origin_id AS person_id,
  link_origin_id,
  link_rcvd_id,
  link_type,
  link_summary,
  created_at,
  updated_at
 FROM links_data 

 UNION

 SELECT  id,
   link_rcvd_id,
   link_origin_id,
   link_rcvd_id,
   link_type,
   link_summary,
   created_at,
   updated_at
 FROM links_data

The view works fine. Note that the view creates an additional column 'person_id' which is not in the underlying table.

I am running into trouble creating postgres rules that will edit the underlying table.

Specifically, I can successfully edit a table view when it is not a UNION view. But below is what occurs when I try to write a rule with a UNION view:

CREATE RULE inverse_links AS ON INSERT TO links DO INSTEAD
INSERT INTO links_data
  (id, link_origin_id, link_type, link_summary, link_rcvd_id, created_at, 
   updated_at)

VALUES (nextval('people_id_seq'), new.link_origin_id, new.link_type, 
        new.link_summary, new.link_rcvd_id, new.created_at, new.updated_at)

RETURNING *;

The above rule should redirect the edits to the underlying table 'links_data'.

But I am getting the following error:

ERROR:  RETURNING list's entry 3 has different type from column "link_origin_id"

********** Error **********

ERROR: RETURNING list's entry 3 has different type from column "link_origin_id"
SQL state: 42P17

The 2 things I feel might be the problem is that 1) the view has an additional column which is causing the column types to not match up or 2) there might be something with the fact that the table is a UNION on itself and editing this might be a problem.

Any idea of where I can go with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-09-15 03:33:02

免责声明:几乎没有可更新视图的经验。

您的 RETURNING 子句是问题所在 - links_data 表很可能有 7 列(如您的视图定义和 ON INSERT 规则所示)并且您正在使用 RETURNING * 返回刚刚插入的行的内容,但您的视图有 8 列。检查并同步这两个列表。

Disclaimer: almost no experience with updatable views.

Your RETURNING clause is the problem - the links_data table most probably has 7 colums (as shown by your view definition and the ON INSERT rule) and you're returning their contents for the just inserted row with RETURNING * but your view has 8 columns. Check and sync those two lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文