Re: Upsert error "column reference is ambiguous"

From: Tim Starling <tstarling(at)wikimedia(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Upsert error "column reference is ambiguous"
Date: 2025-04-28 22:36:18
Message-ID: 5fe1d1c9-662e-405e-aaed-21d15bdbea06@wikimedia.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 28/4/25 23:54, Tom Lane wrote:
> AFAIK, "ON CONFLICT" is a Postgres-ism. Exactly which constructs
> in exactly which other databases are you citing as precedent?

There's a list here:

<https://wiki.postgresql.org/wiki/UPSERT#UPSERT_as_implemented_in_practice>

Since that page was written in 2014, SQLite added upsert support,
consciously following PG's syntax, except that unqualified names
resolve to target rows.

My code would be like

function upsert( $table, $names, $values, $key, $set ) {
if ( $this->type === 'mysql' ) {
$conflict = 'ON DUPLICATE KEY UPDATE';
} else {
$conflict = "ON CONFLICT ($key) DO UPDATE SET";
}
return $this->query( "INSERT INTO $table ($names) " .
"VALUES ($values) $conflict $set" );
}

The parameters are a little bit more structured than that, but that
gives you the idea.

MediaWiki has supported MySQL's ON DUPLICATE KEY UPDATE since 2013,
and we've always had the conflict target parameter $key since then as
a helper for emulation. So it's trivial to produce either MySQL and
SQLite syntax.

-- Tim Starling

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tim Starling 2025-04-28 22:48:03 Re: Upsert error "column reference is ambiguous"
Previous Message Adrian Klaver 2025-04-28 15:34:00 Re: Fwd: SQL transactions executing from VMSS