PostgreSQL在冲突中更新仅在某些情况下

时间:2019-04-17 14:10:04

标签: postgresql upsert

我有一张这样的桌子

create table public.incominglog
(
    envelopeid        varchar(36) default ''::character varying not null,
    messagetype       text        default NULL::character varying,
    initialenvelopeid varchar(36) default NULL::character varying,
    processid         varchar(36) default NULL::character varying,
    logtimestamp      timestamp,
    customscode       varchar(8),
    exchtypeid        integer
);
create unique index incominglog_envelopeid_uindex
    on public.incominglog (envelopeid);
create unique index incominglog_initialenvelopeid_uindex
    on public.incominglog (initialenvelopeid);

我正在尝试插入这样的值

INSERT INTO
    incominglog (
        envelopeid,
        messagetype,
        initialenvelopeid,
        processid,
        logtimestamp,
        customscode,
        exchtypeid
    )
VALUES
    (
        'ae2a2b46-ae4f-42a1-ada3-1f8f0aff7361',
        'CMN.00001',
        'aad06a96-667f-42c9-9196-8e0fec103d8b',
        '4fed3854-e1de-42eb-b2c7-3ad714b58a9e',
        '2019-04-17 14:57:54.195588',
        '10210130',
        '19200'
    ) 
ON CONFLICT (initialenvelopeid) 
DO update set
    envelopeid = excluded.envelopeid,
    messagetype = excluded.messagetype,
    logtimestamp = excluded.logtimestamp

请帮助我更改插入脚本,如果'CMN.00001'中已经有messagetype具有相同的initialenvelopeid,它将不会更新值。

1 个答案:

答案 0 :(得分:3)

您可以向WHERE添加ON CONFLICT子句:

ON CONFLICT DO UPDATE ...
   WHERE incominglog.messagetype <> 'CMN.00001'