Re: Trouble referencing a multi-column unique constraint by name in ON CONFLICT clause

From: Charles Leifer <coleifer(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trouble referencing a multi-column unique constraint by name in ON CONFLICT clause
Date: 2018-09-27 18:48:50
Message-ID: CAPukbqxebie1K7YX03E2K9Gjyyi2dqtXoOeR+e1zj1BdWLwngg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Many thanks, sorry for missing something so obvious!

On Thu, Sep 27, 2018 at 1:45 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Charles Leifer <coleifer(at)gmail(dot)com> writes:
> > I'm running into behavior I don't understand when trying to do an UPSERT
> > with Postgres. The docs would seem to indicate that the conflict target
> of
> > the INSERT statement can be either an index expression or a constraint
> > name. However, when attempting to reference the constraint name, I get a
> > "column ... does not exist" error.
>
> What I see in the INSERT reference page is
>
> where conflict_target can be one of:
>
> ( { index_column_name | ( index_expression ) } [ COLLATE collation ] [
> opclass ] [, ...] ) [ WHERE index_predicate ]
> ON CONSTRAINT constraint_name
>
> So you can write a parenthesized list of column names, or you can write
> "ON CONSTRAINT constraint_name". Given your second example with
>
> create table kv (
> key text,
> value text,
> extra text,
> constraint kv_key_value unique(key, value));
>
> either of these work for me:
>
> regression=# insert into kv (key, value, extra) values ('k1', 'v1', 'e1')
> on conflict (key, value) do update set extra=excluded.extra;
> INSERT 0 1
> regression=# insert into kv (key, value, extra) values ('k1', 'v1', 'e1')
> on conflict on constraint kv_key_value do update set
> extra=excluded.extra;
> INSERT 0 1
>
> regards, tom lane
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2018-09-27 21:41:48 Re: [GENERAL] Postgre compatible version with RHEL 7.5
Previous Message Tom Lane 2018-09-27 18:45:08 Re: Trouble referencing a multi-column unique constraint by name in ON CONFLICT clause