POSTGRES-处理多个ON CONFLICT约束/索引 [英] POSTGRES - Handling several ON CONFLICT constraints/indexes

查看:1351
本文介绍了POSTGRES-处理多个ON CONFLICT约束/索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表中具有以下内容:

  CONSTRAINT unique_position UNIQUE(id,city,type)

 创建UNIQUE INDEX unique_position_sat_null ON public。 position(id,city),其中类型为null 

发生插入冲突时

  ON CONFLICT ON CONSTRAINT unique_position DO UPDATE SET 
... ..

如何将unique_position和unique_position_sat_null合并到在冲突中进行约束



尝试过:

 在约束上发生冲突unique_position做更新集
.....,
在冲突时发生约束unique_position_sat_null做更新集
.....,

 在约束上冲突unique_position和unique_position_sat_null做更新SE T 
.....,

谢谢。

解决方案

不幸的是,唯一索引和唯一约束在Postgres中并不相同。



定义唯一索引而不是约束,Postgres不会创建具有相同名称的约束(在INSERT期间发生冲突的情况下,会产生错误唯一约束违反,提及索引的名称,但这是一个小错误,请参见 https://www.postgresql.org/message-id/flat/CAH2_zh_cn
$ b

因此,您不能使用索引名称,因为它是约束名称。



您可以执行ALTER TABLE ..添加缺点TRAINT .. UNIQUE,但不能唯一。



因此,这可以认为是Postgres中尚未实现的功能- ON CONFLICT子句需要学习如何使用索引名或ALTER TABLE ..添加约束.. UNIQUE需要打补丁以允许部分约束。



我建议在pgsql-hackers @邮件列表中进行讨论(例如如果您回答了上面提到的线程,并解释了您的问题,那将非常好。)


Have the following in the table where:

CONSTRAINT unique_position UNIQUE (id,city,type)

and

CREATE UNIQUE INDEX unique_position_sat_null ON public."position" (id,city) where type is null

on insert conflict there is currently:

ON CONFLICT ON CONSTRAINT unique_position DO UPDATE SET
        .....

How can I incorporate both unique_position and unique_position_sat_null into ON CONFLICT ON CONSTRAINT

Tried:

ON CONFLICT ON CONSTRAINT unique_position DO UPDATE SET
            .....,
ON CONFLICT ON CONSTRAINT unique_position_sat_null DO UPDATE SET
            .....,

and

ON CONFLICT ON CONSTRAINT unique_position and unique_position_sat_null  DO UPDATE SET
            .....,

Thank you.

解决方案

Unfortunately, unique indexes and unique constraints are not the same things in Postgres.

When you define a unique index, not constraint, Postgres doesn't create a constraint with the same name (in case of collisions during INSERTs it will produce error "unique constraint violation" mentioning index' name, but this is a minor bug, see https://www.postgresql.org/message-id/flat/CAH2-Wzn-uXcLgC5uFbqe2rUfmJWP9AxKnMKAEgqU26hbURxk5A%40mail.gmail.com#CAH2-Wzn-uXcLgC5uFbqe2rUfmJWP9AxKnMKAEgqU26hbURxk5A@mail.gmail.com)

So you cannot use index name like it is a constraint name.

You could do ALTER TABLE .. ADD CONSTRAINT .. UNIQUE, but it cannot be unique.

So this can be considered as a not yet implemented feature in Postgres -- either "ON CONFLICT" clause needs to learn how to use index names or ALTER TABLE .. ADD CONSTRAINT .. UNIQUE needs to be patched to allow partial constraints.

I suggest discussing it in pgsql-hackers@ mailing list (e.g. if you answer thread mentioned above, explaining your problem, it would be awesome).

这篇关于POSTGRES-处理多个ON CONFLICT约束/索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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