This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-23
Channels
- # announcements (2)
- # babashka (25)
- # beginners (33)
- # biff (13)
- # calva (13)
- # clerk (82)
- # clj-commons (3)
- # clj-kondo (8)
- # clj-on-windows (23)
- # cljdoc (6)
- # clojure (16)
- # clojure-belgium (1)
- # clojure-dev (58)
- # clojure-europe (53)
- # clojure-nl (1)
- # clojure-norway (15)
- # clojure-uk (2)
- # clojurescript (17)
- # core-async (5)
- # cursive (6)
- # datahike (1)
- # datomic (8)
- # emacs (25)
- # etaoin (21)
- # events (4)
- # graalvm (33)
- # honeysql (7)
- # hyperfiddle (1)
- # lsp (49)
- # luminus (4)
- # malli (18)
- # off-topic (63)
- # reagent (11)
- # releases (1)
- # shadow-cljs (200)
- # timbre (1)
- # tools-build (17)
Hi all, does anyone know how to use postgres' on-conflict
stuff where the update should increment a value from honeysql.
Something like:
(honey/format
{:insert-into :table
:values [{:id "id" :counter 1}]
:on-conflict [:id]
:do-update-set [:+ :counter 1]})
this is made up, but results in:
["INSERT INTO table (id, counter) VALUES (?, ?) ON CONFLICT (id) DO UPDATE SET + = EXCLUDED.+, counter = EXCLUDED.counter, ? = EXCLUDED.?"
"id"
1
1
1]
whereas I want something like:
["INSERT INTO table (id, counter)
VALUES (?, ?)
ON CONFLICT (id)
DO UPDATE SET
counter = table.counter + ?"
"id"
1
1]
Probably a bit specific or unusual as a case, but just in case!You want the hash map form of :do-update-set
:
user=> (sql/format {:insert-into :table :values [{:id "id" :counter 1}] :on-conflict [:id] :do-update-set {:counter [:+ :counter 1]}})
["INSERT INTO table (id, counter) VALUES (?, ?) ON CONFLICT (id) DO UPDATE SET counter = counter + ?" "id" 1 1]
user=>
If you specifically need table.counter
:
user=> (sql/format {:insert-into :table :values [{:id "id" :counter 1}] :on-conflict [:id] :do-update-set {:counter [:+ :table.counter 1]}})
["INSERT INTO table (id, counter) VALUES (?, ?) ON CONFLICT (id) DO UPDATE SET counter = table.counter + ?" "id" 1 1]
user=>
The vector form of :do-update-set
only sets excluded columns.
ha, amazing, thanks @U04V70XH6 that was speedy! I appreciate the help, I did look through some docs FWIW and didn't notice this option https://cljdoc.org/d/seancorfield/honeysql/2.0.0-rc2/doc/getting-started/postgresql-support#upsert
Can you create an issue on GH for me to make that clearer? I suspect there's a test showing it, but it really should be in the docs too... Thanks.
🙏 2