user3112185
user3112185

Reputation: 101

How to insert clj-time date-time objects on postgresql with korma

In order to avoid dealing with java's date and time objects I'm using clj-time. When I use it with the korma's sqlite3 helper it works fine, but when I do something like

(insert posts (values {:name "name" :due_date (date-time 2014)}))

I'm told the system can't infer the object type.

I've been looking around and asking, and it seems that the clojure.java.jdbc supports a protocol called ISQLValue precisely to deal with things like this. It is used to extend objects so they can represent themselves to the database. However the latest version of korma uses a 0.2 release of CJJ.

Is there any alternative to using clj-time.coerce/to-sql-date every time a date-time object needs to be presented to a psql database?

Upvotes: 0

Views: 1426

Answers (1)

piyushmandovra
piyushmandovra

Reputation: 4381

i am using following function

clj-time.coerce/to-sql-time

and it's working fine for me. I tested dry-run of insert query

(use :reload-all 'clj-time.core)
(use :reload-all 'clj-time.coerce)

and then

(defn sql-now[]
  "Returns now in sql time formate"
  (to-sql-time (now)))

and then

(use :reload-all 'korma.core)

and then

(dry-run (insert :users (values {:email "[email protected]" :created_at (sql-now)})))

and ouput is

dry run :: INSERT INTO "users" ("created_at", "email") VALUES (?, ?) :: [#inst "2014-09-15T06:32:24.498000000-00:00" [email protected]]
=> [{nil 1}]

it's working fine for me please check these if it's full fill your requirement. if you have any doubt please reply back?

it's working fine for me

Upvotes: 1

Related Questions