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...
This is the clojure code:
(-> (insert-into :records)
(values sample-records)
(sql/format))
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
can you show what your sample-records
looks like
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.> 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 inSo the SQL comes out (?, ?, ?, ?, ?, ?, 'food')
or something
Yup, that did it, thanks for the prompt!
you're welcome 🙂
@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.
That make sense, thanks Sean