0

I would like to perform upsert sql statement in cockroach DB.

I would like to insert row into table when specific id does not exist in table and update name column when id with given value exist in table.

Table definition:

CREATE TABLE IF NOT EXISTS programs_tiers (
  id STRING PRIMARY KEY,
  program_id STRING NOT NULL REFERENCES programs (id),
  name STRING,
  created TIMESTAMPTZ DEFAULT current_timestamp(),
  UNIQUE(program_id, name)
);

Here is my sql statement using knex.

 const response =  await this.knex.raw('INSERT INTO programs_tiers (id,name) values(?,?) ON CONFLICT(id) DO UPDATE SET name = excluded.name  WHERE program_id = ? AND id = ?',[tierId, tierName, programId, tierId]);

I'm getting following error:

error: column reference "id" is ambiguous (candidates: excluded.id, programs_tiers.id).

How could i solve this ?

spruser
  • 13
  • 3

1 Answers1

1

Specify the table name to fix the ambiguity:

[...] WHERE program_id = ? AND programs_tiers.id = ?
                               ^^^^^^^^^^^^^^^
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109