Re: How do I upsert depending on a second table? - Mailing list pgsql-general

From Adrian Klaver
Subject Re: How do I upsert depending on a second table?
Date
Msg-id bdef948d-27cb-46b4-9a6e-857c7e1e0d58@aklaver.com
Whole thread Raw
In response to How do I upsert depending on a second table?  (Samuel Marks <samuelmarks@gmail.com>)
Responses Re: How do I upsert depending on a second table?
List pgsql-general
On 9/23/25 13:36, Samuel Marks wrote:
> Attempt:
> ```sql
> CREATE TABLE org
> (
>      "name"      VARCHAR(50) PRIMARY KEY,
>      owner       VARCHAR(50) NOT NULL
> );
> 
> CREATE TABLE repo
> (
>      "id"           INTEGER PRIMARY KEY,
>      full_name      VARCHAR(255) UNIQUE NOT NULL,
>      org            VARCHAR(50)         NOT NULL REFERENCES org ("name")
> );
> 
> INSERT INTO org(name, owner) VALUES ('org0', 'user0');
> 
> INSERT INTO repo (id, full_name, org)
> VALUES (0, 'org0/name0 by wrong user', 'org0')
> ON CONFLICT (full_name) DO UPDATE
>      SET full_name = EXCLUDED.full_name,
>          org       = EXCLUDED.org
> WHERE EXISTS (SELECT 1
>                FROM org org_tbl
>                WHERE org_tbl.name = EXCLUDED.org
>                  AND org_tbl.owner = 'wrong user')

Where is org_tbl?

Or is this a copy and paste error?

> RETURNING *;
> 
> SELECT * FROM repo WHERE id = 0;
> ```
> 
> This all succeeds. It should fail because the 'wrong user' is trying
> to create a new—or update an existing—repo.
> 
> Thanks for all suggestions
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com



pgsql-general by date:

Previous
From: Samuel Marks
Date:
Subject: How do I upsert depending on a second table?
Next
From: Samuel Marks
Date:
Subject: Re: How do I upsert depending on a second table?