I am using Postgres. I'd like to write an upsert function that will do an update when there is already a row that exists. The table has a unique index:
create unique index if not exists unique_constraint
on my_table (col1, col2, col3) nulls not distinct;
I am struggling with the syntax for this in HoneySQL. I have this:
{:insert-into :my_table
:values [row]
:on-conflict {:on-constraint :unique_constraint}
:do-update-set (dissoc row :created_at)}
Which fails; I've also tried various over incantations like this, which also fail:
{:insert-into :my_table
:values [row]
:on-conflict [:col1 :col2 :col3]
:do-update-set (dissoc row :created_at)}
I suspect there's something simple I am overlooking here. Any suggestions?you want something like
{:insert-into :my_table
:values [(into (sorted-map) row)]
:on-conflict [:col1 :col2 :col3]
:do-update-set {:fields (sort (keys (dissoc row :created_at)))}
This works:
{:insert-into :my_table
:values [row]
:on-conflict [:col1 col2 col3]
:do-update-set {:fields (keys (dissoc row :created_at))}}(The issue was unrelated to HoneySQL syntax; I have a test fixture that is old and uses a different jdbc than our HoneySQL tooling uses, and the transactions were colliding in a confusing error message.)