tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.5k stars 309 forks source link

Uncaught Error: compare on non-nil objects of different types #38

Closed scttnlsn closed 9 years ago

scttnlsn commented 10 years ago

I'm getting this error occasionally when calling transact!. The only pattern I could find possibly has something to do with the underscores in the following example...

This always throws an error:

(d/transact! conn [{:db/id -1 :test/foo "foo" :test/_bar {:baz "qux"}}])

But these always work fine:

(d/transact! conn [{:db/id -1 :test/foo "foo" :test/bar {:baz "qux"}}])
(d/transact! conn [{:db/id -1 :test/_bar {:baz "qux"}}])
(d/transact! conn [{:db/id -1 :test/foo "foo" :test/_bar 123}])

Here's the JavaScript stack trace:

compare (core.cljs:1521)
cmp (core.cljs:44)
cmp_datoms_eavt (core.cljs:48)
binary_search_l (btset.cljs:33)
_seek (btset.cljs:382)
_slice (btset.cljs:404)
datascript.btset.slice.slice__3 (btset.cljs:414)
datascript.btset.slice.slice (btset.cljs:412)
datascript.btset.slice.slice__2 (btset.cljs:411)
datascript.btset.slice.slice (btset.cljs:412)
datascript.core.DB.datascript$core$ISearch$_search$arity$2 (core.cljs:80)
_search (core.cljs:31)
transact_add (core.cljs:192)
transact_tx_data (core.cljs:249)
with$ (datascript.cljs:37)
(anonymous function) (datascript.cljs:45)
cljs.core.swap_BANG_.swap_BANG___2 (core.cljs:3348)
cljs.core.swap_BANG_.swap_BANG_ (core.cljs:3358)
_transact_BANG_ (datascript.cljs:44)
transact_BANG_ (datascript.cljs:51)

Any ideas?

tonsky commented 10 years ago

Underscores sure are a problem. When you write [:db/add X :test/_bar Y] it reads backwards, meaning “put a reference from Y to X via :test/bar property”. So it expects both X and Y to be integers. In your case, you put hashmap at Y pos, and that wouldn’t work as an entity id.

So two problems for me to fix here:

  1. Error message should be improved (sorry about that)
  2. Error should be thrown in second case (you don’t get it here by pure luck):

    (d/transact! conn [{:db/id -1 :test/_bar {:baz "qux"}}])

scttnlsn commented 10 years ago

@tonsky Okay, thanks. I'll have to read the Datomic docs about this. I don't get any error when there's only a single attribute (like no. 2 above).

tonsky commented 10 years ago

datomic allows to specify maps when creating nested relation, but ds does not support this feature yet. what about backward relation, I'm no sure that even datomic supports that=