Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v1A3k-005BAp-N3 for pgsql-general@arkaria.postgresql.org; Tue, 23 Sep 2025 20:56:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v1A3j-0083ED-Cd for pgsql-general@arkaria.postgresql.org; Tue, 23 Sep 2025 20:56:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v1A3j-0083E4-1q for pgsql-general@lists.postgresql.org; Tue, 23 Sep 2025 20:56:23 +0000 Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v1A3e-002Uyh-3D for pgsql-general@lists.postgresql.org; Tue, 23 Sep 2025 20:56:22 +0000 Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-58120c35d47so155980e87.1 for ; Tue, 23 Sep 2025 13:56:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758660978; x=1759265778; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0JnTRWPtDw2hFcxPJNZ6d4EwZmP/BfyD7VFTjx6kVRM=; b=N/kb9/yu6uToy7IXwmki5RqoziGiKibApFs6Wjsy66Qx8cA3OCmxzQNRWBvUpvRIvR mjKgGHO6uFA3hpTCujzAaE7PJeMl9AB4MUlx/wpJVzFn/eBLcA9rhSSTdOOtdU0GhY5v Gy82hG1HmV5u3yAusmhe5eMEZG5U8CnQOAa1ICV6umcSODvMvsTR+g4MGCScGze+sIa7 4Ly0S4UEHlh08tbH9A7VMXGiUmkQeZDcNAXOIzGbmlt0D/ci07NVqeqK6VpFu1Ps0C3V kvLMraIloiuW37OSwa4XMKF/JrT5fkqqdfOFCkpD7YfyRSYVjbl7D/SP7P9WmQc692yv oppg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758660978; x=1759265778; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0JnTRWPtDw2hFcxPJNZ6d4EwZmP/BfyD7VFTjx6kVRM=; b=MrGL/Tv/F+2T4sLtskzFPcPkc9tjRXim7Vfzd7dy5qn14PQkKBg6Wcr69ujq/Gmab9 aiUPQV1n4L/ihyP5W5q8dOWNEHZfyMIuW7pb0+d/Z0bg08gIiqie2yjkCgk4aYmAfuYQ xmqxrJsl5SOHavZpOrDhGcPR/lOCcoRJLnWnIkVD4RGSEBk49RsaKk0XRrKzurPGvF96 pJu1j4TsHcOsgm3xpI4bzn+mz96uPqAgx8N2jye9WAIfwtE2LFjtb/HLcUGvHPn3LS2O 8RmM9YvZwuReXWaulq/jKk5+TkWZ9SSmP8EuGGhRxjMNekscjuWlzfgIi0SzMi7RT/yA S5IQ== X-Gm-Message-State: AOJu0YxuDkiKhlDd5TgqOG3lb7V7Fjt0aYBGnY+6sIGKmLNb4f77P3kc QpFdP8BJlytSyWRosQb/ZnOj+5h2icbBcgYjluv4MfFjpqgt4SDSAgpnqI+BBwUSpZ5Wso9GOMB cWCpj1MPxjVwCY0nSnUpa5NZOzXugeCRluPjFjFM= X-Gm-Gg: ASbGncunJfHhJOd3m7hzrgmVb2Z6nlcaYNlswpTaDcPISBEJ2RGE2mbHNS0Q/oXeKUK 7TtGdIvDMc5GlhYxiTY1A2cqqwboLR8ErHSoWVPISTlyv/0fzEAy6x0Gg0cWDN+vOGQAvaVSsEz gsfS1wz0Yt2NmRPmhGAGpNNcIyFt3gVwf3NpRCPNLeT9WQFbetA+5Av6/GkuPsFIYA1CPb4ULiL IH1cwPg X-Google-Smtp-Source: AGHT+IEISXHyFVN1bBUc97nxGkbl6VK2mEkFlU9Mz7J48S9KcUqWZ0XdQ1AqPd6G32gnDuag/ZaaGNnq4SKXWgrlkjw= X-Received: by 2002:a05:651c:b2b:b0:36b:2a0a:b902 with SMTP id 38308e7fff4ca-36d17bc7ddemr5809831fa.8.1758660977694; Tue, 23 Sep 2025 13:56:17 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Samuel Marks Date: Tue, 23 Sep 2025 15:56:05 -0500 X-Gm-Features: AS18NWAY-936U9dVeE-r5ESuFy7bMIVah8KESdRqX_vOqvhxuoixhZRpxdophD0 Message-ID: Subject: Re: How do I upsert depending on a second table? To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 =3D 1` (I used a space) On Tue, Sep 23, 2025 at 3:52=E2=80=AFPM Adrian Klaver 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 =3D EXCLUDED.full_name, > > org =3D EXCLUDED.org > > WHERE EXISTS (SELECT 1 > > FROM org org_tbl > > WHERE org_tbl.name =3D EXCLUDED.org > > AND org_tbl.owner =3D 'wrong user') > > Where is org_tbl? > > Or is this a copy and paste error? > > > RETURNING *; > > > > SELECT * FROM repo WHERE id =3D 0; > > ``` > > > > This all succeeds. It should fail because the 'wrong user' is trying > > to create a new=E2=80=94or update an existing=E2=80=94repo. > > > > Thanks for all suggestions > > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com