public inbox for [email protected]
help / color / mirror / Atom feedHow do I upsert depending on a second table?
4+ messages / 3 participants
[nested] [flat]
* How do I upsert depending on a second table?
@ 2025-09-23 20:36 Samuel Marks <[email protected]>
0 siblings, 2 replies; 4+ messages in thread
From: Samuel Marks @ 2025-09-23 20:36 UTC (permalink / raw)
To: [email protected]
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')
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: How do I upsert depending on a second table?
@ 2025-09-23 20:52 Adrian Klaver <[email protected]>
parent: Samuel Marks <[email protected]>
1 sibling, 1 reply; 4+ messages in thread
From: Adrian Klaver @ 2025-09-23 20:52 UTC (permalink / raw)
To: Samuel Marks <[email protected]>; [email protected]
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]
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: How do I upsert depending on a second table?
@ 2025-09-23 20:56 Samuel Marks <[email protected]>
parent: Adrian Klaver <[email protected]>
0 siblings, 0 replies; 4+ messages in thread
From: Samuel Marks @ 2025-09-23 20:56 UTC (permalink / raw)
To: Adrian Klaver <[email protected]>; +Cc: [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]
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: How do I upsert depending on a second table?
@ 2025-09-23 21:31 David G. Johnston <[email protected]>
parent: Samuel Marks <[email protected]>
1 sibling, 0 replies; 4+ messages in thread
From: David G. Johnston @ 2025-09-23 21:31 UTC (permalink / raw)
To: Samuel Marks <[email protected]>; +Cc: [email protected] <[email protected]>
On Tuesday, September 23, 2025, Samuel Marks <[email protected]> wrote:
>
>
$subject
You can only upsert/provoke a meaningful conflict on the singular table
being inserted into.
There are other features like functions and triggers that may get you
something usable.
David J.
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2025-09-23 21:31 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-09-23 20:36 How do I upsert depending on a second table? Samuel Marks <[email protected]>
2025-09-23 20:52 ` Adrian Klaver <[email protected]>
2025-09-23 20:56 ` Samuel Marks <[email protected]>
2025-09-23 21:31 ` David G. Johnston <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox