Fork me on GitHub

Hello guys. Can someone point me to the documentation or example of how to work properly with nested entities in datascript? for example, I have an entity parent which has a children property and this property is a reference with cardinality many It’s simple to add the new references but it’s not clear how to delete references (without deleting child entities itself) I will appreciate for any help.


Hello @UF2GC1JRM 👋 This is relatively straightforward using retractions:

(d/transact conn {:tx-data [[:db/retract parent :children child1]])
This would retract the fact that child1 is a child of parent. You would first need to query for the entity ids to obtain both parent and child1, and then use them in the transaction. If you wanted to remove multiple parent/child relationships in one go:
(d/transact conn {:tx-data [[:db/retract parent :children child1]
                            [:db/retract parent :children child2]
                            [:db/retract parent :children child3]]})


Here is a potentially helpful reference page: Keep in mind that the Datomic API docs are usually helpful as well, with a few differences. Cross referencing the Datomic documentation with the datascript wiki is a good strategy.


Hi @U6GFE9HS7. Thanks, will give it a try



(def schema 
  {:thing/parent {:db/unique :db.unique/identity}
  :thing/child {:db/valueType :db.type/ref
               :db/cardinality :db.cardinality/many
               :db/isComponent true}}) 
The key part is the :db/isComponent true


Will usually improve your experience with parent/child stuff


Yes, it could help but in my case from domain modeling perspective the child entity it's not the component of the parent entity


Right. :db/isComponent will cause the child to be retracted along with the parent, which from your problem description is not what you want.


To answer your specific question, to delete a reference from :thing/parent to :thing/child you can simply assert

(d/trasnsact conn 
[[:db/retract [:thing/parent parent-id] :thing/children [:thing/child child-id]])
Not sure if that helps

Daniel Hines22:02:42

Can you define rules in the :where clause? I.e, as opposed to passing them into :in?


Not that I know of…could you give an example of what you’re after?

Daniel Hines15:02:37

I'm using Roam Research, a web app for note taking with built in Datascript queries of your notes. It supports the where clause, but not in. I tried this:

[:find ?content
    [?e :node/title ?title]
    [(re-find #"February 21st" ?title)]
    [(parent ?a ?b)
      [?a :block/children ?b]]
    (parent ?e ?x)
    [?x :block/string ?content] 
The idea is to make a rule called parent that I can use in another rule called ancestor , allowing me to do recursive queries. But I get the following error:
Cannot parse binding, expected (bind-scalar | bind-tuple | bind-coll | bind-rel)