Hi there, I’m reposting my question here
My sample code is as follows:
(ns test-db.core
(:require [java-time :as time]
[next.jdbc.date-time]
[hugsql.adapter.next-jdbc :as next-adapter]
[hugsql.core :as hugsql]))
(hugsql/def-db-fns "test_db/main.sql")
(def db "jdbc:<postgresql://localhost/foobar>")
(defn test1
[]
(insert-user! db {:user-id "222" :updated-at (time/instant)}))
The content of main.sql:
-- :name insert-user! :! :n
INSERT INTO users (user_id, updated_at)
VALUES (:user-id, :updated-at);
Project dependencies:
:dependencies [[org.clojure/clojure "1.10.0"]
[org.postgresql/postgresql "42.2.19"]
[cheshire "5.10.0"]
[clojure.java-time "0.3.2"]
[com.github.seancorfield/next.jdbc "1.2.659"]
[com.layerware/hugsql "0.5.1"]
[com.layerware/hugsql-adapter-next-jdbc "0.5.1"]]
The table was created using:
CREATE TABLE users
(id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id varchar(50),
updated_at timestamp with time zone);
The error I’m getting is:
Execution error (PSQLException) at org.postgresql.jdbc.PgPreparedStatement/setObject (PgPreparedStatement.java:1011).
Can't infer the SQL type to use for an instance of java.time.Instant. Use setObject() with an explicit Types value to specify the type to use.
Can you try doing the insert directly with next.jdbc
to take HugSQL out of the mix?
Let me try that
Ah, it works directly with next.jdbc, so something funny is going on with hugsql
There’s a #hugsql channel if that helps.
(I’ll be curious to find out what the problem is)
Thanks @seancorfield Anyway it seems to work after I set the adapter
(hugsql/set-adapter! (next-adapter/hugsql-adapter-next-jdbc))
Oh, it would be using clojure.java.jdbc
I think without you doing that…
Yes, that’s correct, after double-checking the document, that is what it is doing
Thanks for the help!
There’s a HugSQL quick start in the next.jdbc
docs BTW https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.2.659/doc/getting-started/friendly-sql-functions#hugsql-quick-start
Yes, I did the setting of the adapter earlier, but took that code out while debugging some other issue
Actually I was trying to get next.jdbc to run with duct but I was having an issue so I removed the adapter line (so it ends up it was calling clojure.jdbc instead)
(defmethod ig/init-key :trade-sniffer/crawler
[_ {:keys [logger] {db :spec} :db :as config}]
(next.jdbc.sql/insert! db :users {:user_id "222" :updated_at (time/instant)}))
This results in an error:
next.jdbc.sql/insert! sql.clj: 190
next.jdbc.sql/insert! sql.clj: 192
next.jdbc/execute-one! jdbc.clj: 198
next.jdbc.protocols/eval21234/fn/G protocols.clj: 33
next.jdbc.result-set/eval21823/fn result_set.clj: 609
next.jdbc.protocols/eval21172/fn/G protocols.clj: 14
next.jdbc.connection/eval22955/fn connection.clj: 258
next.jdbc.connection/spec->url+etc connection.clj: 192
clojure.lang.ExceptionInfo: Unknown dbtype:
datasource: #object[net.ttddyy.dsproxy.support.ProxyDataSource 0x51ce4746 "net.ttddyy.dsproxy.support.ProxyDataSource@51ce4746"]
The same db value works with clojure.jdbc thoughWhere (:db config)
has the following value:
duct.database.sql.Boundary{:spec {:datasource #object[net.ttddyy.dsproxy.support.ProxyDataSource 0x307dad3d "net.ttddyy.dsproxy.support.ProxyDataSource@307dad3d"]}}
I think I have figured it out, it needs one more redirection down to :datasource
So instead of (insert! db xxx)
it had to be (insert! (:datasource db) xxx)
That suggests that whatever you're using to set up the datasource is using (or at least assuming) clojure.java.jdbc