sql

All things SQL and JDBC...
practicalli-john 2021-05-25T21:31:32.030Z

I create a simple function with insert! inside a doseq

(defn persist-all-subscription-results
  [subscription-results]
  (with-open [connection (jdbc/get-connection db-spec)]
    (doseq [result subscription-results]
      (sql/insert! connection :analysis result))))
This does insert the first hash-map in the database. If I read this correctly, it seems postgres does not like the result and generates an error
org.postgresql.util.PSQLException
   ERROR: syntax error at or near "-" Position: 30

    QueryExecutorImpl.java: 2553  org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse
    QueryExecutorImpl.java: 2285  org.postgresql.core.v3.QueryExecutorImpl/processResults
    QueryExecutorImpl.java:  323  org.postgresql.core.v3.QueryExecutorImpl/execute
          PgStatement.java:  481  org.postgresql.jdbc.PgStatement/executeInternal
          PgStatement.java:  401  org.postgresql.jdbc.PgStatement/execute
Any pointers ?? Did I do something silly?

dpsutton 2021-05-25T21:33:59.030200Z

shooting from the hip you have snake-case and need to underscore into snake_case

seancorfield 2021-05-25T21:35:50.030400Z

@jr0cket If you’re using the latest next.jdbc, there should be a next.jdbc/snake-kebab-opts that you can pass into insert! as a final argument to do that automatically.

practicalli-john 2021-05-25T21:57:02.031Z

Oh yes, I should add the snake-kebab-opts thing as another argument to insert! I seemed to have though next.jdbc did it automatically, wooops...

practicalli-john 2021-05-25T22:03:22.031200Z

Success. Thank you all.

seancorfield 2021-05-25T22:08:11.031400Z

I think c.j.j did too many automatic adjustments to column names both going in and out so next.jdbc defaults to doing nothing and leaves it up to users. If everything you are doing is with kebab-case <-> snake_case then you can use with-options to create a connectable that automatically applies those options to operations (caveat: with-transaction and get-connection return unadorned Java objects that need wrapping via with-options again in those contexts).