Postgres:关于与隐式冲突目标的冲突

Postgres: on conflict with implicit conflict target

Postgres 10 和 11 插入规范说:

ON CONFLICT [ conflict_target ] conflict_action

我有一个 table:

create table c (
    e text not null,
    m text not null,
    v numeric not null,
    PRIMARY KEY (e, m)
)

我也想做

insert into candle values (...) 
    on conflict do update set
            v = 5

但我得到一个错误:

ON CONFLICT DO UPDATE requires inference specification or constraint name Hint: For example, ON CONFLICT (column_name)

为什么我必须提供一个冲突的目标?如何提供主键或其他一些列集?

您可以有多个约束,因此有多个备选操作。

您可以简单地指定列名:

insert into candle values (...) 
on conflict (e,m) do update set
  v = 5

是的,<em>conflict_target</em> 是可选的,但只有 DO NOTHING.

根据 the documentation:

For ON CONFLICT DO NOTHING, it is optional to specify a <em>conflict_target</em>; when omitted, conflicts with all usable constraints (and unique indexes) are handled. For ON CONFLICT DO UPDATE, a <em>conflict_target</em> must be provided.