Fork me on GitHub
#honeysql
<
2021-11-10
>
beders07:11:23

hey, there, quite new to honeysql (but not SQL). Here’s two examples that are supposedly doing the same thing, however, only the second one seems to create the right SQL:

(let [data '({:installer-id        "bubu"
              :states              (sql-types/array ["CA" "DC"])
              :improvement-type-id #uuid"7efc8a9e-a317-424f-a775-ba4bf6a5bf94"
              })] 
  (-> (hsql/insert-into :installer-improvement-types)
      (hsql/values data)
      (sql/format :pretty true)))
=>
["INSERT INTO installer_improvement_types (installer_id, states, improvement_type_id) VALUES (?, array ? DC, ?)"
 "bubu"
 "CA"
 #uuid"7efc8a9e-a317-424f-a775-ba4bf6a5bf94"]

beders07:11:00

(-> (hsql/insert-into :installer-improvement-types)
      (hsql/columns :installer-id :states :improvement-type-id)
      (hsql/values [["bubu" (sql-types/array ["CA" "DC"]) #uuid"7efc8a9e-a317-424f-a775-ba4bf6a5bf94"]])
      (sql/format :pretty true))
=>
["INSERT INTO installer_improvement_types (installer_id, states, improvement_type_id) VALUES (?, ARRAY[?, ?], ?)"
 "bubu"
 "CA"
 "DC"
 #uuid"7efc8a9e-a317-424f-a775-ba4bf6a5bf94"]

beders07:11:56

what am I doing wrong when using the map format for values? Thanks in advance

seancorfield16:11:15

In the first form you've quoted the whole expression so sql-types is not evaluated.

seancorfield16:11:07

(let [data [{:installer-id        "bubu"
             :states              (sql-types/array ["CA" "DC"])
             :improvement-type-id #uuid"7efc8a9e-a317-424f-a775-ba4bf6a5bf94"
             }]] 
  (-> (hsql/insert-into :installer-improvement-types)
      (hsql/values data)
      (sql/format :pretty true)))
That should do what you want @beders

seancorfield16:11:17

(no quote, use a vector instead of a list)