Hi, I am trying to update a table in a postgres db with next-jdbc using the update! fn as follows:
(defn ->table [s]
(let [_ (debug (str "->table:" s))]
(str/replace s #"-" "_")))
(defn ->col [s]
(let [_ (debug (str "->col:" s))]
(str/replace
(str/replace s #"-" "")
#"\."
"_")))
(defn update-tasks [db tasks]
(let [ds (jdbc/get-datasource db)
results (f/try*
(doseq
[t tasks]
(sql/update! ds :my-tasks t {:table-fn ->table
:column-fn ->col})))]
(if (f/failed? results)
results
tasks)))
but get this exception:
Can't infer the SQL type to use for an instance of api.resources.db$__GT_table. Use setObject() with an explicit Types value to specify the type to use.
org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of vms_api.resources.db$__GT_table. Use setObject() with an explicit Types value to specify the type to use.
at org.postgresql.jdbc.PgPreparedStatement.setObject (PgPreparedStatement.java:978)
I'm not sure, how the :table-fn is getting down into the postgres jdbc driver as a value. I am using this successfully with an insert! fn that looks almost identical.
Am I doing something wrong here?@pdmct You have the wrong arguments to update!
It takes a connectable (`ds`), a table (`:my-tasks`), a map of updates (`t`), a where map or clause -- which is missing -- and then then options.
ok, I see
thanks
update!
takes one more argument than insert!
🙂
(I must admit, I was confused by that error message at first!)
yes, that isn't obvious , I have been scratching my head for while on this. so some more like this:
(sql/update! ds :analytics-tasks t {:id (:id t)}
{:table-fn ->table
:column-fn ->col}))
that's much better
Yup, that will perform the update WHERE id = ?
... (:id t)
cool, thanks
The problem is that it couldn't tell the difference between the map {:id (:id t)}
and the map {:table-fn ->table :column-fn ->col}
since both are maps and options are ... optional.
So even next.jdbc.specs
couldn't have helped you here.
yes, I see that now. thanks for the quick response
got it :thumbsup: