Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem UPSERTing with partial unique indexes #125

Open
S4G4R opened this issue Jul 31, 2022 · 0 comments
Open

Problem UPSERTing with partial unique indexes #125

S4G4R opened this issue Jul 31, 2022 · 0 comments

Comments

@S4G4R
Copy link

S4G4R commented Jul 31, 2022

Given a table definition such as :

create table sample_relation
(
  id    text not null,
  b_id  int  not null,
  c_id  int,

  unique(id, b_id, c_id),
  foreign key (id)   references sample_relation_2(id),
  foreign key (b_id) references sample_relation_3(id),
  foreign key (c_id) references sample_relation_3(id)
);

and a partial unique index defined on it :

create unique index idx_sample_relation_id_b_id
           on sample_relation(id, b_id)
        where c_id is null;

I would like to UPSERT some data. I have tried doing that in the following way :

(sql/sql
 (sql/insert db :sample-relation []
             (sql/values {:id "abc"
                          :b-id 1
                          :c-id 2})
             (sql/on-conflict [:id :b-id]
                              (sql/where '(is-null c-id))
                              (sql/do-update {:c-id :EXCLUDED.c-id}))))

which should translate to this query :

insert into sample_relation(id, b_id, c_id)
     values ('abc', 1, 2)
on conflict (id, b_id)
      where c_id is null
  do update set c_id = EXCLUDED.c_id;

but instead wrongly translates to (Note the position of the WHERE clause) :

insert into sample_relation(id, b_id, c_id)
     values ('abc', 1, 2)
on conflict (id, b_id)
  do update set c_id = EXCLUDED.c_id
      where c_id is null;

which in turn gives me the error :

column reference "c_id" is ambiguous

Is my syntax wrong or is this a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant