public inbox for [email protected]  
help / color / mirror / Atom feed
From: Samuel Marks <[email protected]>
To: Adrian Klaver <[email protected]>
Cc: [email protected]
Subject: Re: How do I upsert depending on a second table?
Date: Tue, 23 Sep 2025 15:56:05 -0500
Message-ID: <CAMfPbcZzUYZ6yoN-TnpNQZ4PzhFCCiTdchbtwz83+B0Byyk-fw@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CAMfPbcbjTE6=yVJjAyiNU=kM24e-0oLo8S1ZZ7dJZy3UKq+b3g@mail.gmail.com>
	<[email protected]>

the AS syntax can alternatively be used for aliases
https://www.postgresql.org/docs/current/sql-select.html

`SELECT actual_tablename table0 WHERE table0.column00 = 1`

(I used a space)

On Tue, Sep 23, 2025 at 3:52 PM Adrian Klaver <[email protected]> wrote:
>
> 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
> [email protected]






view thread (4+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: How do I upsert depending on a second table?
  In-Reply-To: <CAMfPbcZzUYZ6yoN-TnpNQZ4PzhFCCiTdchbtwz83+B0Byyk-fw@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox