Skip to content

Instantly share code, notes, and snippets.

@cigitia
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cigitia/feafd70768fcd551cd16 to your computer and use it in GitHub Desktop.
Save cigitia/feafd70768fcd551cd16 to your computer and use it in GitHub Desktop.
DataScript 0.9.0 demonstration of possible bug in entity-ID generation when creating new component entities within transaction
(ns demo.main
(:require [datascript :as ds]))
(def schema
{:x/component {:db/valueType :db.type/ref ; Required to demonstrate bug.
:db/isComponent true ; Required to demonstrate bug.
:db/cardinality :db.cardinality/many
; Not required but demonstrates what happens to ID numbering
; after first component.
}})
(def conn
(ds/create-conn schema))
(ds/transact! conn
[{:x/component [{:c/attribute "blah"} {:c/attribute "bar"}]}
{:y/completely-different "foo"}])
(enable-console-print!)
(prn @conn)
; Will print something like:
; #datascript/DB {:schema ...
; :datoms [[1 :c/attribute "blah" 536870913]
; [1 :x/component 1 536870913]
; [1 :x/component 2 536870913]
; [2 :c/attribute "bar" 536870913]
; [3 :y/completely-different "foo" 536870913]]}
; The IDs of the new components of the first entity (i.e., values of [1 :x/component _]) are 1 and 2,
; referring to the non-component entities created in the same transaction, rather than separate
; component entities. In particular, the [1 :x/component 1] is an unexpected circular reference.
; (The circular reference from 1 to 1 causes an infinite loop when coupled with another bug
; when using the Push API on entity 1.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment