Skip to content

Commit

Permalink
Fix issue 678: Make it possible to store :db/id as a keyword value (#679
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jonasseglare authored Jun 8, 2024
1 parent 78fbfb2 commit 17a22d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/datahike/db/transaction.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,12 @@
(assoc v (dbu/reverse-ref a-ident) eid)
(if reverse?
[:db/add v straight-a eid]
[:db/add eid straight-a (if (and attribute-refs? (ds/is-system-keyword? v)) ;; translation of system enums
(dbi/-ref-for db v)
v)])))]
[:db/add eid straight-a
(if (and attribute-refs?
(dbu/is-attr? db straight-a-ident :db/systemAttribRef)
(ds/is-system-keyword? v)) ;; translation of system enums
(dbi/-ref-for db v)
v)])))]
(if ensure
(let [{:keys [:db.entity/attrs :db.entity/preds]} (-> db :schema ensure)]
(if (empty? attrs)
Expand Down
5 changes: 5 additions & 0 deletions src/datahike/db/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@
:db.cardinality/many [:db.cardinality/many]
:db.type/ref [:db.type/ref :db/index]
:db.type/tuple [:db.type/tuple]

:db.type/valueType [:db/systemAttribRef]
:db.type/cardinality [:db/systemAttribRef]
:db.type/unique [:db/systemAttribRef]

(if (= k :db/ident)
[:db/ident]
(when (true? v)
Expand Down
18 changes: 18 additions & 0 deletions test/datahike/test/attribute_refs/differences_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@
#{:name}))
(d/release conn)))))

(deftest test-store-db-id-as-keyword
(doseq [cfg (init-cfgs)]
(testing "Do not resolve enums in keyword DB"
(let [conn (setup-db cfg)]
(d/transact conn [{:db/ident :attribute-to-use
:db/cardinality :db.cardinality/one
:db/valueType :db.type/keyword}])
(d/transact conn [{:attribute-to-use :the-location}
{:attribute-to-use :db/id}])
(is (= #{[:the-location]
[:db/id]}
(d/q '[:find ?v
:in $ ?a
:where
[_ :attribute-to-use ?v]]
@conn)))
(d/release conn)))))

(deftest test-indexing
(let [[no-ref-cfg ref-cfg] (init-cfgs)
schema [{:db/ident :name
Expand Down

0 comments on commit 17a22d8

Please sign in to comment.