加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 指向重复约束值的Upsert错误(On Conflict Do Upda

发布时间:2020-12-13 16:05:13 所属栏目:百科 来源:网络整理
导读:当我尝试在FROM语句中使用多个源时,我在Postgres 9.5中遇到ON CONFLICT DO UPDATE的问题. 工作代码示例: INSERT INTO new.bookmonographs (citavi_id,abstract,createdon,edition,title,year)SELECT "ID","Abstract","CreatedOn"::timestamp,"Edition","Tit
当我尝试在FROM语句中使用多个源时,我在Postgres 9.5中遇到ON CONFLICT DO UPDATE的问题.

工作代码示例:

INSERT INTO new.bookmonographs  (citavi_id,abstract,createdon,edition,title,year)
SELECT "ID","Abstract","CreatedOn"::timestamp,"Edition","Title","Year"
FROM old."Reference"
WHERE old."Reference"."ReferenceType" = 'Book'
    AND old."Reference"."Year" IS NOT NULL
    AND old."Reference"."Title" IS NOT NULL
ON CONFLICT (citavi_id) DO UPDATE 
    SET (abstract,year) = (excluded.abstract,excluded.createdon,excluded.edition,excluded.title,excluded.year)
;

错误的代码:

INSERT INTO new.bookmonographs  (citavi_id,"Year"
FROM old."Reference",old."ReferenceAuthor"
WHERE old."Reference"."ReferenceType" = 'Book'
    AND old."Reference"."Year" IS NOT NULL
    AND old."Reference"."Title" IS NOT NULL
    AND old."ReferenceAuthor"."ReferenceID" = old."Reference"."ID"
    --Year,Title and Author must be present in the data,otherwise the entry is deemed useless,hence won't be included
ON CONFLICT (citavi_id) DO UPDATE 
    SET (abstract,excluded.year)
;

我在FROM语句中添加了一个额外的源,还有一个WHERE语句,以确保只有具有title,year和author的条目插入到新数据库中. (如果旧的话.“引用”.“ID”存在于旧的中.“ReferenceAuthor”作为“ReferenceID”,然后作者存在.)即使没有附加的WHERE语句,查询也是错误的.我在SELECT中指定的列只出现在old.“Reference”中,而不是旧的.“ReferenceAuthor”.
目前陈旧.“ReferenceAuthor”和旧的.“参考”没有独特的约束,书籍专用的唯一约束是:

CONSTRAINT bookmonographs_pk PRIMARY KEY (bookmonographsid),CONSTRAINT bookmonographs_bookseries FOREIGN KEY (bookseriesid)
      REFERENCES new.bookseries (bookseriesid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,CONSTRAINT bookmonographs_citaviid_unique UNIQUE (citavi_id)

错误PSQL抛出:

ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time
HINT: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
********** Error **********

ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time
SQL state: 21000
Hint: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.

我不知道什么是错的,或者为什么提示指向重复的约束值.

解决方法

问题是由于显然某些条目有多个作者.因此,您编写的select查询中的内部联接将为同一条目返回多行并且INSERT … ON CONFLICT不喜欢这样.由于您只使用ReferenceAuthor表进行过滤,因此您只需重写查询,以便它使用该表仅通过在相关子查询上执行存在来过滤没有任何作者的条目.这是如何做:

INSERT INTO new.bookmonographs  (citavi_id,"Year"
FROM old."Reference"
WHERE old."Reference"."ReferenceType" = 'Book'
    AND old."Reference"."Year" IS NOT NULL
    AND old."Reference"."Title" IS NOT NULL
    AND exists(SELECT FROM old."ReferenceAuthor" WHERE old."ReferenceAuthor"."ReferenceID" = old."Reference"."ID")
    --Year,excluded.year)
;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读