honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
Joe 2021-04-09T10:22:27.119600Z

Hello, first time HoneySQL user here. I'm doing an insert statement on a PostGres DB, and having trouble with the SQL HoneySQL is spitting out...

Joe 2021-04-09T10:22:51.119700Z

This is the clojure code:

(-> (insert-into :records)
    (values sample-records)
    (sql/format))

Joe 2021-04-09T10:24:33.119900Z

which produces this (slightly sanitized)

["INSERT INTO records (date, balance, account_number, amount, memo, payee, category) VALUES (?, ?, ?, ?, ?, ?, food), (?, ?, ?, ?, ?, ?, food)"
 "01/02/2021"
 123.45
 "acc"
 123.45
 "memo"
 "Deliveroo"
 "01/02/2021"
 123.45
 "acc"
 123.45
 "memo"
 "Tesco"]
When I try to execute this I get
; Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2553).
; ERROR: column "food" does not exist

dharrigan 2021-04-09T10:25:46.120100Z

can you show what your sample-records looks like

Joe 2021-04-09T10:25:55.120300Z

I'm not super clear on why food is being considered a column, when it's clearly specified as a value. But when I manually reformat to

["INSERT INTO records (date, balance, account_number, amount, memo, payee, category) VALUES (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?)"
 "01/02/2021"
 123.45
 "acc"
 123.45
 "memo"
 "Deliveroo"
 "food"
 "01/02/2021"
 123.45
 "acc"
 123.45
 "memo"
 "Tesco"
 "food"]
it seems to work OK.

Joe 2021-04-09T10:27:53.120500Z

> can you show what your sample-records looks like Sure, it's

{:date "01/02/2021"
   :balance 123.45
   :account-number "acc"
   :amount 123.45
   :memo "memo"
   :payee "Deliveroo"
   :category :food}
Now I look at it, it seem like it must be something to do with the keyword. Maybe I need stringify that before I pass it in

Joe 2021-04-09T10:28:21.120700Z

So the SQL comes out (?, ?, ?, ?, ?, ?, 'food') or something

Joe 2021-04-09T10:29:10.120900Z

Yup, that did it, thanks for the prompt!

dharrigan 2021-04-09T10:31:43.121100Z

you're welcome 🙂

seancorfield 2021-04-09T16:14:09.121300Z

@allaboutthatmace1789 Keywords (and symbols) are treated as column names, universally, in HoneySQL — and you would need to convert :food to a string even if you weren’t using HoneySQL because you can’t write a Clojure keyword into a SQL database.

Joe 2021-04-09T16:38:12.121500Z

That make sense, thanks Sean