Fork me on GitHub
#datascript
<
2020-04-10
>
Vishal Gautam08:04:45

Hi, I am working on an app where I am using data script to manage app state. I have a problem who h I don’t know how to solve. I have an entity whose attributes has a cardinal it’s of many and it’s a reference to some other entity, I am trying to add/ remove some of the entity Id but I am not getting what I wanted. Anyone has answer to this. Thank you :)

Aron08:04:09

can you share schema

Aron08:04:13

or example

Vishal Gautam15:04:45

(def schema {:user/favourites {:db/cardinality :db.cardinality/many :db/type :db.type/ref}})

// When adding - WORKS
(d/transact! conn [[:db/add uid :user/favourites fav-id]])

// When retracting - DOESNT WORK
(d/transact! conn [[:db/retract uid :user/favourites fav-id]])

lilactown15:04:14

that works for me

lilactown15:04:15

cljs.user> (require '[datascript.core :as ds])
nil
cljs.user> (def schema {:user/favourites {:db/cardinality :db.cardinality/many :db/type :db.type/ref}})
#'cljs.user/schema
cljs.user> (def conn (ds/create-conn schema))
#'cljs.user/conn
cljs.user> (ds/transact! conn [[:db/add 1 :user/favourites 52]])
#datascript.db.TxReport{:db-before #datascript/DB {:schema {:user/favourites {:db/cardinality :db.cardinality/many, :db/type :db.type/ref}}, :datoms []}, :db-after #datascript/DB {:schema {:user/favourites {:db/cardinality :db.cardinality/many, :db/type :db.type/ref}}, :datoms [[1 :user/favourites 52 536870913]]}, :tx-data [#datascript/Datom [1 :user/favourites 52 536870913 true]], :tempids {:db/current-tx 536870913}, :tx-meta nil}
cljs.user> (ds/touch (ds/entity @conn 1))
{:user/favourites #{{:db/id 52}}, :db/id 1}
cljs.user> (ds/transact! conn [[:db/retract 1 :user/favourites 52]])
#datascript.db.TxReport{:db-before #datascript/DB {:schema {:user/favourites {:db/cardinality :db.cardinality/many, :db/type :db.type/ref}}, :datoms [[1 :user/favourites 52 536870913]]}, :db-after #datascript/DB {:schema {:user/favourites {:db/cardinality :db.cardinality/many, :db/type :db.type/ref}}, :datoms []}, :tx-data [#datascript/Datom [1 :user/favourites 52 536870914 false]], :tempids {:db/current-tx 536870914}, :tx-meta nil}
cljs.user> (ds/touch (ds/entity @conn 1))
{:db/id 1}

Vishal Gautam15:04:47

nvm i wasnt passing the right entity id

lilactown15:04:13

happens to the best of us