Fork me on GitHub
#honeysql
<
2023-06-20
>
Jakub Holý (HolyJak)08:06:54

Hello again! I am at a loss, again 😅 How do I generate

... ON CONFLICT (date_trunc('month', "my_column")) DO  ...
? https://cljdoc.org/d/com.github.seancorfield/honeysql/2.4.1033/api/honey.sql.helpers?q=on-conf#on-conflict does not help me… 🙏 🙏 🙏

p-himik08:06:10

Doesn't seem like there's a way to do it without putting the whole ON CONFLICT into :raw, so might be something that warrants HoneySQL's DSL extension.

👍 2
🙏 2
seancorfield18:06:56

@U0522TWDA I'm fixing this so that this works:

user=> (sql/format {:on-conflict [[:date_trunc [:inline "month"] :my_column]]} {:quoted true})
["ON CONFLICT DATE_TRUNC('month', \"my_column\")"]

seancorfield18:06:55

Does it specifically need those ( .. ) around the expression to be valid syntax?

p-himik19:06:12

It does, yeah.

seancorfield19:06:46

Here's how it works now on the latest develop or the latest SNAPSHOT on Clojars:

user=> (sql/format {:on-conflict [:a [:date_trunc [:inline "month"] :my_column] :b]} {:quoted true})
["ON CONFLICT (\"a\", DATE_TRUNC('month', \"my_column\"), \"b\")"]

seancorfield19:06:44

I'll cut a new release before the end of the month. Not sure what else I'll end up fixing in that release right now.

p-himik19:06:01

Not sure how to test it properly given that I can't use date_trunc in ON CONFLICT without also creating an index on it, and I can't create an index on it because the function is not marked as immutable. But given the syntax description of INSERT, it seems that you need an extra pair of parents around expressions in ON CONFLICT. Just tried it with col + 1 expression instead of date_trunc - yep, needs extra parens, so the above should be formatted as ON CONFLICT ("a", (DATE_TRUNC('month', "my_column")), "b"). If it's any help, at least in the case of PostgreSQL the whole part after ON CONFLICT follows the CREATE INDEX format as per the docs.

seancorfield19:06:32

Oh, interesting... it needs parens inside the group too??

seancorfield19:06:50

(and, no, matching create index doesn't help at all 🙂 )

p-himik19:06:16

Yeah, otherwise stuff like INSERT INTO t VALUES (1) ON CONFLICT (d + 1) DO NOTHING; fails with ERROR: syntax error at or near "+" Position: 41. Using ((d + 1)) works.

seancorfield20:06:48

Fix pushed to GH. A new SNAPSHOT will be available "soon".

Jakub Holý (HolyJak)06:06:35

Sorry for the late reply. I see you figured everything out. Many thanks to both of you! I'll try the snapshot ASAP

Jakub Holý (HolyJak)12:06:40

It works like a charm! thank you!

🎉 1