Search code examples
postgresqlbulkinsertupsertbulkupdate

How to eliminate ambiguous column reference error in bulk upsert for postgresql?


For the following code I am trying to do a bulk upsert into a table. The table is called jobstep_to_step_relationships. If the primary key column "job_base_step_id" already exists in the table I want the algorithm to do an update instead.

create table jobstep_to_step_relationships

    (
        job_base_step_id uuid default uuid_generate_v4() not null
            constraint jobstep_to_step_relationships_pkey
                primary key,
        step_id uuid not null,
        parent_job_base_step_id uuid not null,
        parent_step_id uuid not null,
        job_id uuid not null,
        order_number integer not null,
        is_group boolean not null,
        created_at bigint not null,
        updated_at bigint not null
    );




INSERT INTO jobstep_to_step_relationships
(job_base_step_id, step_id, parent_job_base_step_id, parent_step_id, job_id, order_number,
 is_group, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9),
       ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (job_base_step_id)
    DO UPDATE SET job_base_step_id        = EXCLUDED.job_base_step_id,
                  step_id                 = EXCLUDED.step_id,
                  parent_job_base_step_id = EXCLUDED.parent_job_base_step_id,
                  parent_step_id          = EXCLUDED.parent_step_id,
                  job_id                  = EXCLUDED.job_id,
                  order_number            = EXCLUDED.order_number,
                  is_group                = EXCLUDED.is_group,
                  created_at              = EXCLUDED.created_at,
                  updated_at              = EXCLUDED.updated_at
WHERE EXCLUDED.job_base_step_id = job_base_step_id
  AND EXCLUDED.step_id = step_id

However for the follwing posted code I am getting this error:

[2019-06-25 13:17:47] [42702] ERROR: column reference "job_base_step_id" is ambiguous

Not sure what I'm interpreting wrong from the documentation. Can someone point out what is wrong?


Solution

  • Specify the table name to fix the ambiguity:

    WHERE EXCLUDED.job_base_step_id = jobstep_to_step_relationships.job_base_step_id
      AND EXCLUDED.step_id = jobstep_to_step_relationships.step_id