Hello. I am trying to figure out how to insert enum values in PostgreSQL with next.jdbc and honeysql. Everything works fine when I use plain next.jdbc with calling as-other as described here: https://github.com/seancorfield/next-jdbc/blob/develop/doc/tips-and-tricks.md#working-with-enumerated-types But I am not able to insert enum with honeysql
(-> (h/insert-into :table)
(h/values (map #(update :enum-field (comp jdbc-types/as-other name)) records))
(hsql/format))
[3:24 PM] this code throws exception java.lang.AssertionError Assert failed: Alias should have two parts@michal.kurtak Shouldn't there be a %
in that anon function?
(map #(update % :enum-field (comp jdbc-types/as-other name)) records)
sory this is just a copy from real code, where is i use threading macro
@michal.kurtak OK, I've repro'd and I think you're running into this known bug https://github.com/seancorfield/honeysql/issues/267
I haven't yet figured out the best solution for this. The problem is that the jdbc-types/as-*
functions have to wrap the value in a vector so metadata can be applied (so a custom set-parameter
implementation is used), but HoneySQL doesn't like vector values due to a bug in how it tries to parse hash maps.
(defn- scrape->row
[scrape]
(-> scrape
(update :scrape/artists (partial into-array String))
(update :scrape/web-scraper (comp jdbc-types/as-other name))))
(defn insert-scrapes!
[db scrapes]
(db/execute! db (-> (h/insert-into :scrape)
(h/values (map scrape->row scrapes))
(hsql/format))))
this is the full code
hmm
i have workaround it without honeysql
(defn insert-scrapes-no-honey!
[db scrapes]
(let [columns [:scrape/external-ref-id
:scrape/title
:scrape/artists
:scrape/url
:scrape/description
:scrape/published
:scrape/rating
:scrape/web-scraper]
rows (->> scrapes
(map scrape->row)
(map (apply juxt columns))
(vec))]
(jdbc-sql/insert-multi! db :scrape columns rows jdbc/snake-kebab-opts)))
You could simplify that, I think, with columns (keys (first scrapes))
?
(assuming your maps always contain all those keys and only those keys)
I personally don't find HoneySQL adds much value for inserts but it is definitely a bug that it fails to handle vector values in hash maps in values
.
well, there are other keys in scrape so i cannot use this simplification
and what about the recommendation you’ve gave in the issue? “Can you try using `{:identifier 123 :genres #sql/array ["Genre"]}`, which should generate”
That would help for :scrape/artists
which you are passing as an array, but it won't help for as-other
It's just a bug in HoneySQL that I need to figure out how to fix (without breaking anything else).
My recommendation for now is: use next.jdbc
directly for inserts.
Ok, i understand and will use next.jdbc for inserts. Thank you for your help