Re: Upsert error "column reference is ambiguous"

From: Tim Starling <tstarling(at)wikimedia(dot)org>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, 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-29 11:07:47
Message-ID: ac2caa5c-9f18-49db-8f6a-91cd1131f36e@wikimedia.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 29/4/25 16:36, Laurenz Albe wrote:
> On Tue, 2025-04-29 at 08:36 +1000, Tim Starling wrote:
>> 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.
>
> Another litle "if" to cater for PostgreSQL's "EXCLUDED." would be
> such a big problem?

I don't understand what you mean. EXCLUDED is not needed. "$table."
needs to be prefixed to every column reference in the string $set. How
do you find the column references amongst the string literals,
function calls, etc.? You would need to parse the expression.

This is a public interface and there may be callers in code that I
don't have access to.

Part of the reason for wanting to replace the existing emulation with
a native upsert is to simplify the code. Parsing the expression is
definitely not a simplification.

-- Tim Starling

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Rowley 2025-04-29 12:43:17 Re: Upsert error "column reference is ambiguous"
Previous Message Duygu Hasan 2025-04-29 10:43:49 Pg client certificate auth