diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f859325..e7ba66d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - fix in Pull API for reverse non-component attributes (#91, thx [Matt Senior](https://github.com/mattsenior)) - Node.js and Browser repls for dev profile (#93) - Preconditions to validate db/conn arguments (#101) +- Id allocation bug (#66) # 0.11.5 diff --git a/src/datascript/core.cljc b/src/datascript/core.cljc index e4cc2c33..aaceea4f 100644 --- a/src/datascript/core.cljc +++ b/src/datascript/core.cljc @@ -908,8 +908,10 @@ new-eid (or (:db/id upserted) (next-eid db)) new-entity (assoc upserted :db/id new-eid) new-report (cond - (nil? old-eid) (allocate-eid report new-eid) - (shim/neg-number? old-eid) (allocate-eid report old-eid new-eid) + (nil? old-eid) (allocate-eid report new-eid) + (shim/neg-number? old-eid) (allocate-eid report old-eid new-eid) + (and (number? old-eid) + (> old-eid (:max-eid db))) (allocate-eid report old-eid) :else report)] (recur new-report (concat (explode db new-entity) entities))) diff --git a/test/datascript/test/explode.cljc b/test/datascript/test/explode.cljc index fb88da7e..7558fe03 100644 --- a/test/datascript/test/explode.cljc +++ b/test/datascript/test/explode.cljc @@ -100,3 +100,37 @@ [ {:email "@2" :_profile [{:name "Ivan"} {:name "Petr"} ]} ] #{ [1 :email "@2"] [2 :name "Ivan"] [2 :profile 1] [3 :name "Petr"] [3 :profile 1] } )))) + +(deftest test-circular-refs + (let [schema {:comp {:db/valueType :db.type/ref + :db/cardinality :db.cardinality/many + :db/isComponent true}} + db (d/db-with (d/empty-db schema) + [{:db/id 1, :comp [{:name "C"}]}])] + (is (= (mapv (juxt :e :a :v) (d/datoms db :eavt)) + [ [ 1 :comp 2 ] + [ 2 :name "C"] ]))) + + (let [schema {:comp {:db/valueType :db.type/ref + :db/cardinality :db.cardinality/many}} + db (d/db-with (d/empty-db schema) + [{:db/id 1, :comp [{:name "C"}]}])] + (is (= (mapv (juxt :e :a :v) (d/datoms db :eavt)) + [ [ 1 :comp 2 ] + [ 2 :name "C"] ]))) + + (let [schema {:comp {:db/valueType :db.type/ref + :db/isComponent true}} + db (d/db-with (d/empty-db schema) + [{:db/id 1, :comp {:name "C"}}])] + (is (= (mapv (juxt :e :a :v) (d/datoms db :eavt)) + [ [ 1 :comp 2 ] + [ 2 :name "C"] ]))) + + (let [schema {:comp {:db/valueType :db.type/ref}} + db (d/db-with (d/empty-db schema) + [{:db/id 1, :comp {:name "C"}}])] + (is (= (mapv (juxt :e :a :v) (d/datoms db :eavt)) + [ [ 1 :comp 2 ] + [ 2 :name "C"] ])))) +