Good morning, I am trying to figure out how to insert data into DB, with this code:
(jdbc/insert! db "auth_token" {:id "35cf7140-b2d4-4b5d-9067-70fdae337a99"
:account-id "ffffffff-ffff-ffff-ffff-000000000100"
:token "7e58bcb3-198c-4884-998a-d3df08f1cb73"
:email "<mailto:tony@example.com|tony@example.com>"
:expires-at "2021-06-06T08:57:14.544792"}))
And I get the error:
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2553).
ERROR: syntax error at or near "-"
Position: 36
Any way to print out the query string to see where there is an issue?@timofey.sitnikov Because you have kebab-case
key names.
Those are not valid in SQL.
Are you using next.jdbc
?
If so, you can leverage the snake-kebab-opts
(if you’re using the latest version of next.jdbc
).
@seancorfield, ahhh... thank you, that worked. I do use next.jdbc.
. How do you use snake-kebab-opts
? I did find it https://github.com/seancorfield/next-jdbc/blob/8d02bd68f06a25300011532fed6dedab731f5990/src/next/jdbc.clj#L401 , but cannot figure out how to use it.
It’s just options. You pass it into next.jdbc
calls.
It causes kebab-case
keys to be converted to snake_case
columns (and tables) and vice versa when result sets come back.
OK, like so?
(sql/insert! ds :address {:name "A. Person" :email "<mailto:albert@person.org|albert@person.org>"} {:snake-kebab-opts true})
No, it’s an options map.
(sql/insert! ds :address {:name "A. Person" :email "<mailto:albert@person.org|albert@person.org>"} jdbc/snake-kebab-opts)
Although in this case there’s no point: you don’t have any kebab-case
keys.I guess the docs are not clear enough. Suggestions on how to improve them?
In addition, two pre-built option hash maps are available in next.jdbc, that leverage the camel-snake-kebab library:
snake-kebab-opts -- provides :column-fn, :table-fn, :label-fn, :qualifier-fn, and :builder-fn that will convert Clojure identifiers in :kebab-case to SQL entities in snake_case and will produce result sets with qualified :kebab-case names from SQL entities that use snake_case,
unqualified-snake-kebab-opts -- provides :column-fn, :table-fn, :label-fn, :qualifier-fn, and :builder-fn that will convert Clojure identifiers in :kebab-case to SQL entities in snake_case and will produce result sets with unqualified :kebab-case names from SQL entities that use snake_case.
(this is in Getting Started, just after an example showing an options map with :builder-fn
)
Has anyone used next.jdbc/with-logging
on the develop branch yet? Any feedback on it?
Yeah we've (team of 3) used in in development since the announcement to tap> queries and results. So far it has been a blessing! We tap queries before execution + a promise for the results, which are delivered in the second fn. (That way broken statements which result in exceptions still tap their value). Needed to play around with Datafy to remove those promises from the view, but over all 👍:skin-tone-2:
That would certainly help! I think the exception in the result slot should be fine for my usecase at least
My team uses lein git down just for this feature right now, happy to bump early and let you know how it works out :)