replikativ / datahike

A fast, immutable, distributed & compositional Datalog engine for everyone.
https://datahike.io
Eclipse Public License 1.0
1.62k stars 95 forks source link

Fix issue 678: Make it possible to store :db/id as a keyword value #679

Closed jonasseglare closed 2 months ago

jonasseglare commented 2 months ago

SUMMARY

Fixes #678

The code in this PR makes it possible to run the following transactions against an empty database with attribute-refs turned on:

        (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}])

Currently, in the main branch, this is not possible and results in an exception being thrown. The fundamental issue is that a part of the code in transact.cljc tries to convert keywords that are system keywords to their database ids. It tries to do this mapping no matter the value type of the attribute of the datom being transacted. For example, if the value type is a keyword that just happens to have the same name as a system attribute, it will still attempt to map, which is wrong. And this specifically fails for :db/id which is not an attribute in the database but is a system keyword. The line that fixes this issue is (dbu/is-attr? db straight-a-ident :db/systemAttribRef) in transaction.cljc which will make sure that we only attempt this mapping to database ids if the value type of the attribute is a type that makes sense to map.

Checks

Bugfix