Search code examples
clojuredatomic

Datomic query on UUID field not returning expected result


I'm persisting an entity to Datomic, and I can pull it back out as expected. The entity has a unique UUID field :student/id, and if I try to query with a match on that field's value, I'm not getting results back. What am I doing wrong?

(require '[datomic.api :as d])

(def uri "datomic:mem://db")

(d/create-database uri)

(def conn (d/connect uri))
(def db (d/db conn))

(def schema
  [;; students
   {:db/ident :student/uuid1
    :db/valueType :db.type/string
    :db/cardinality :db.cardinality/one}
   {:db/ident :student/last
    :db/valueType :db.type/string
    :db/cardinality :db.cardinality/one}
   {:db/ident :student/id
    :db/valueType :db.type/uuid
    :db/cardinality :db.cardinality/one
    :db/unique :db.unique/identity}
   ])

(d/transact conn schema)

;; students

(def brandon {:student/id (d/squuid)
              :student/uuid1 "Brandon"
              :student/last "Smith"})

(def brandon-tx (d/transact conn [brandon]))
(def new-db (:db-after @brandon-tx))

@(def uuid1 (:student/id brandon))
;; ==>#uuid "64ce962a-e750-4d5e-a7eb-c860156ee8d0"
@(def uuid2 (-> (d/q '[:find (pull ?e [*])
                       :where [?e :student/id]] new-db) ffirst :student/id))
;; ==>#uuid "64ce962a-e750-4d5e-a7eb-c860156ee8d0"

(= uuid2 uuid1)
;; => true

;; I'm expecting to get Brandon back here, but I get an empty set
(d/q '[:find ?e
       :where [?e :student/id uuid1]] new-db)
;; => #{}

Solution

  • This is exactly what "inputs" are for, in Datomic queries.

    Here is a link to the relevant documentation:

    hth