Fork me on GitHub
Mario C.03:06:22

Quick question: The Clojure JDBC docs state that the get-by-id function will return a single matching row, or nil. When I run the method it returns a list with a single map inside ({:some-key "JDBC"}). Is this what it's suppose to return or should it have returned just the map? I want to know whether the list was introduced by the JDBC wrapper itself or something I did on my end.

Mario C.03:06:30

I ask because I don't have the code in front of me but I would like to have an idea on what to look for tomorrow.


@mario Can you share the code you used to invoke it?

Mario C.03:06:01

@seancorfield I can't because I don't have access to it at the moment

Mario C.03:06:23

(get-by-id db table pk-value) We are using that format to invoke it


user=> (def db-spec {:dbtype "h2:mem" :dbname "example"})
user=> (require '[ :as jdbc])
user=> (jdbc/db-do-commands db-spec (jdbc/create-table-ddl :example [[:name "varchar(32)"] [:value :int]]))
user=> (jdbc/insert! db-spec :example {:name "one" :value 1})
user=> (jdbc/insert! db-spec :example {:name "two" :value 2})
user=> (jdbc/insert! db-spec :example {:name "three" :value 3})
user=> (jdbc/find-by-keys db-spec :example {:name "two"})
({:name "two", :value 2})
user=> (jdbc/get-by-id db-spec :example "two" :name)
{:name "two", :value 2}
^ @mario.cordova.862


You should only ever get a single hash map back from get-by-id...

Mario C.03:06:02

@seancorfield Thank you very much! Although I am a little confused on how you invoked the method. You called it with (jdbc/get-by-id db-spec :example "two" :name). Where as we just did (jdbc/get-by-id db-spec :example id) where id is an integer.

Mario C.03:06:36

Although this tells me that the issue is on our end!

Mario C.03:06:22

hmm is it stating that the pk will be :name in this case?

Mario C.03:06:41

Yes just referenced the docs! Thanks again!


now that's service!

Mario C.04:06:33

This man deserves a medal!


he also maintains that library. he's quite a boon for the clojure community

Mario C.04:06:25

Yes, I always see him helping on here


@mario.cordova.862 Yeah, I was just being a bit lazy at the REPL and didn't create a column called :id 🙂


The less lazy version

user=> (jdbc/db-do-commands db-spec (jdbc/create-table-ddl :keyed [[:id :int "not null auto_increment"] [:name "varchar(32)"] [:value :int]]))
user=> (jdbc/insert! db-spec :keyed {:name "one" :value 1})
({:id 1})
user=> (jdbc/insert! db-spec :keyed {:name "two" :value 2})
({:id 2})
user=> (jdbc/insert! db-spec :keyed {:name "three" :value 3})
({:id 3})
user=> (jdbc/query db-spec ["select * from keyed"] {:result-set-fn clojure.pprint/print-table})

| :id | :name | :value |
|   1 |   one |      1 |
|   2 |   two |      2 |
|   3 | three |      3 |
user=> (jdbc/get-by-id db-spec :keyed 2)
{:id 2, :name "two", :value 2}
There's the 3-arg version with a table that has a primary key called :id @mario.cordova.862 🙂


Anyone familiar with orchestra? Any idea why (orchestra.spec.test/instrument) will return an empty vector when there are s/fdefs available in the namespace?


Guys, I didn’t find in the java-time API documentation.. how to convert (local-date-time) or (java.time.LocalDateTime/now) to unix timestamp. I tried in several methods.. any idea? (I tried with clj-time.coerce/to-long etc)


@sb Does this help? (System/currentTimeMillis)


No, that isn’t local time. I know that.


That is an application.


So you want it in a particular timezone?


yes, exactly.


You mentioned that you tried with clj-time.coerce/to-long. Were you getting the correct DateTime object before that?


Some code would help


(.toEpochMilli (jt/instant)) i got.. minus 2 hours (becuase time zones), with (.toEpochMilli (jt/local-date-time)) the error message IllegalArgumentException No matching field found: toEpochMilli for class java.time.LocalDateTime clojure.lang.Reflector.getInstanceField (


(.getEpochSecond (java.time.Instant/from (java.time.ZonedDateTime/now))) ;; 1528814124


java time is saying that without an offset from UTC, it doesn't know how to match up the local time to the epoch time. If you start with a zoned date time, you can create an instant and then get the epoch second. You could look into how to get a zoned date time from your local date time or start from the zoned date time if possible


@dpsutton utton thanks the help.. yes in this case, I don’t have a quick solution.


ah. i was looking to see how to go local -> zoned but i didn't see anything obvious. back to the docs


atZone is what you are looking for (if this plan works for you of course :)


@dpsutton thx the help, I think so!! 👍👏