honeysql

jcd 2025-05-20T22:38:43.607789Z

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?

2025-05-20T22:44:19.681589Z

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)))}

jcd 2025-05-20T22:55:20.311389Z

This works:

{:insert-into :my_table
         :values [row]
         :on-conflict [:col1 col2 col3]
         :do-update-set {:fields (keys (dissoc row :created_at))}}

👍 1
jcd 2025-05-20T23:04:28.065639Z

(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.)

👍 1