Search code examples
clojuredatomic

In Datomic, how to add new entities (as references) to the many attribute of an existing entity


By way of example I'm going to use a drawing as the existing entity which is to have two new shapes added to it. This is the tx that is failing:

[{:shape/id "BKF806TXXTAFWII0", :db/id "22NF08ZVGH9N7QGG_0"}
 {:shape/id "YKIZU4CJC0JXJKVQ", :db/id "22NF08ZVGH9N7QGG_1"}
 [:db/add 17592186047451 :drawing/shapes ["22NF08ZVGH9N7QGG_0" "22NF08ZVGH9N7QGG_1"]]]

As you can see the existing drawing has :db/id of 17592186047451. I'm trying to create two new shapes and add them to the cardinality many attribute :drawing/shapes. "22NF08ZVGH9N7QGG_0" and "22NF08ZVGH9N7QGG_1" are tempids that are supposed to be translated to the same new :db/ids wherever they appear.

I should say that the drawing doesn't have any existing shapes. If it did I would expect them to be orphaned.

This is the error message I'm getting:

Execution error (ExceptionInfo) at datomic.client.api.async/ares (async.clj:58). Cannot interpret as a keyword: 22NF08ZVGH9N7QGG_0, no leading :

Something similar I've also tried:

[{:shape/id "9HTL5BMMHT6QUJM6", :db/id "22NF08ZVGH9N7QGG_0"}
 {:shape/id "O5UB9IG9UB8KDVA2", :db/id "22NF08ZVGH9N7QGG_1"}
 [:db/add 17592186047451 :drawing/shapes "22NF08ZVGH9N7QGG_0"]
 [:db/add 17592186047451 :drawing/shapes "22NF08ZVGH9N7QGG_1"]]

Gives this error message:

Execution error (ExceptionInfo) at datomic.client.api.async/ares (async.clj:58). Two datoms in the same transaction conflict {:d1 [17592186047451 :drawing/shapes 17592186049126 13194139538021 true], :d2 [17592186047451 :drawing/shapes 17592186049127 13194139538021 true]}

This gives the same error message as the first attempt:

[{:db/id 17592186047451, :drawing/shapes ["22NF08ZVGH9N7QGG_0" "22NF08ZVGH9N7QGG_1"]}
 {:shape/id "3DZYWHEPQIAELF25", :db/id "22NF08ZVGH9N7QGG_0"}
 {:shape/id "GJ804SOOU36YQX6Y", :db/id "22NF08ZVGH9N7QGG_1"}]

Getting rid of the tempids by doing it in a nested way:

[{:db/id 17592186047451,
  :drawing/shapes
  [{:shape/id "GEMRMRFG0E6N262M"}
   {:shape/id "HTH2C7R90BQFFNXT"}]}]

Just yields this error message:

Execution error (ExceptionInfo) at datomic.client.api.async/ares (async.clj:58). Unable to resolve entity: {:shape/id "GEMRMRFG0E6N262M"}


Solution

  • Your second example

    [{:shape/id "9HTL5BMMHT6QUJM6", :db/id "22NF08ZVGH9N7QGG_0"}
     {:shape/id "O5UB9IG9UB8KDVA2", :db/id "22NF08ZVGH9N7QGG_1"}
     [:db/add 17592186047451 :drawing/shapes "22NF08ZVGH9N7QGG_0"]
     [:db/add 17592186047451 :drawing/shapes "22NF08ZVGH9N7QGG_1"]]
    

    and third example

    [{:db/id 17592186047451, :drawing/shapes ["22NF08ZVGH9N7QGG_0" "22NF08ZVGH9N7QGG_1"]}
     {:shape/id "3DZYWHEPQIAELF25", :db/id "22NF08ZVGH9N7QGG_0"}
     {:shape/id "GJ804SOOU36YQX6Y", :db/id "22NF08ZVGH9N7QGG_1"}]
    

    should work. Perhaps your :drawing/shapes attribute is not :db.cardinality/many?