From 17a22d8a771134d55a63fa9f5b1cd03dfd1cd3fc Mon Sep 17 00:00:00 2001 From: jonasseglare <5850088+jonasseglare@users.noreply.github.com> Date: Sat, 8 Jun 2024 05:56:50 +0200 Subject: [PATCH] Fix issue 678: Make it possible to store :db/id as a keyword value (#679) --- src/datahike/db/transaction.cljc | 9 ++++++--- src/datahike/db/utils.cljc | 5 +++++ .../test/attribute_refs/differences_test.cljc | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/datahike/db/transaction.cljc b/src/datahike/db/transaction.cljc index eac7ad990..4a269ad46 100644 --- a/src/datahike/db/transaction.cljc +++ b/src/datahike/db/transaction.cljc @@ -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) diff --git a/src/datahike/db/utils.cljc b/src/datahike/db/utils.cljc index 2fbe890a5..e6867ef14 100644 --- a/src/datahike/db/utils.cljc +++ b/src/datahike/db/utils.cljc @@ -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) diff --git a/test/datahike/test/attribute_refs/differences_test.cljc b/test/datahike/test/attribute_refs/differences_test.cljc index 0231d4de0..08dc4d01c 100644 --- a/test/datahike/test/attribute_refs/differences_test.cljc +++ b/test/datahike/test/attribute_refs/differences_test.cljc @@ -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