Re: Partial update on an postgres upsert violates constraint

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Andreas Terrius <gotenwinz99(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Kim Rose Carlsen <krc(at)hiper(dot)dk>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Partial update on an postgres upsert violates constraint
Date: 2016-11-21 15:24:13
Message-ID: b6efd71d-14fa-a2af-12be-1f97fecdfd9b@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/21/2016 02:32 AM, Andreas Terrius wrote:
> Is there any way to check whether the row already exists before checking
> constraints ? I still want it to fail if it turns out to be a new row
> (which would violate the not null constraint), but updates the row if it
> already exists.
>
> Since if that is not possible, I would need to do a query to determine
> whether the row exists in the database which kinda eliminates the use of
> upsert. (in this case, partial upsert).

Before UPSERT appeared in 9.5, folks came up of with alternate methods
of doing this. I would suggest searching on:

postgres upsert cte

You might be able to modify the examples to get what you want.

>
>
>
> On Sun, Nov 20, 2016 at 3:57 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us
> <mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us>> wrote:
>
> Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com
> <mailto:adrian(dot)klaver(at)aklaver(dot)com>> writes:
> > ... So looks like constraints are checked before you get to the ON
> CONFLICT section.
>
> Right. ON CONFLICT is a means for dealing with duplicate-key errors in
> the specified (or inferred) unique index. It is *not* an all-purpose
> error catcher. In the case at hand, the given INSERT request fails due
> to not-null constraints that are unrelated to what the ON CONFLICT
> clause
> tests for.
>
> regards, tom lane
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message dhaval jaiswal 2016-11-21 17:34:56 Re: pg_class (system) table increasing size.
Previous Message John McKown 2016-11-21 15:15:11 Re: Postgresql 9.5 and Shell scripts/variables vs. C programming/defining a value to be used