Fork me on GitHub
#honeysql
<
2021-03-10
>
bartuka18:03:42

Hi, I need an extra help with insert-into. I have a data like:

(def my-data [{:value1 "1", :mymap {:map2 "42"}}])
this can potentially be several maps and I want to insert-many.
(jdbc/execute! datasource (-> {:insert-into :Table :values my-data} (sql/format)))
but I got an error
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2553).
ERROR: syntax error at or near ")"
The sql query generate will be
(-> {:insert-into :Table
     :values [{:value1 "1" :mymap {:map2 "42"}}]}
     (sql/format))
;; => ["INSERT INTO Table (value1, mymap) VALUES (?, ())" "1"]

bartuka18:03:13

I feel I am missing something here. Not sure if I need to add something specific into sql/format

seancorfield19:03:45

@iagwanderson Which version of HoneySQL are you using? And what does it mean to have a column whose value is a hash map?

bartuka19:03:19

version "1.0.461", the hashmap is coerced to jsonb fields

seancorfield19:03:59

With 2.0, you can do this:

user=> (-> {:insert-into :Table
  #_=>      :values [{:value1 "1" :mymap [:lift {:map2 "42"}]}]}
  #_=>      (sql/format))
["INSERT INTO Table (value1, mymap) VALUES (?, ?)" "1" {:map2 "42"}]
I'm not sure what the solution is in 1.0, probably wrapping it in (sql/param ..)?

seancorfield19:03:36

Nope, not sql/param

bartuka19:03:49

I tried, sql/param too.

seancorfield19:03:13

I'm not sure if it's possible with 1.0.

seancorfield19:03:47

(well, I'm sure it is possible but I don't know how, off the top of my head)

bartuka19:03:28

I will investigate this further later on and fix my situation now using next.jdbc directly. Would be good idea to create an issue as enhancement to support this? At least for tracking and generate docs if current facilities allow

seancorfield19:03:51

It works in 2.0 as shown above. If it isn't possible in 1.0, it won't get fixed at this point.

👍 3
seancorfield19:03:56

If you figure out how to do it in 1.0, I'd accept a PR for the docs for 1.0, but I'd encourage you you start upgrading to 2.0 at this point: you can depend on both versions at the same time -- different group IDs and different namespaces -- so you could use 2.0 for just that one insert if you wanted, and stay with 1.0 for everything else.

bartuka19:03:16

> If you figure out how to do it in 1.0, I'd accept a PR for the docs for 1.0, but I'd encourage you you start upgrading to 2.0 at this point: you can depend on both versions at the same time -- different group IDs and different namespaces -- so you could use 2.0 for just that one insert if you wanted, and stay with 1.0 for everything else. nice, it makes sense. thank you!