Replace cardinality-many attribute


#1

Say I have an account model with cardinality-many attribute:

{:db/ident       :account/id
  :db/valueType   :db.type/long
  :db/cardinality :db.cardinality/one
  :db/unique      :db.unique/identity}

 {:db/ident       :account/interests
  :db/valueType   :db.type/string
  :db/index       true
  :db/cardinality :db.cardinality/many}

Now I’d like to update the account with new interests. I don’t want to merge them but replace old interests with new ones, e.g.:

{:account/id 42
 :account/interests ["run" "swim"]}

So now the account #42 has only ["run" "swim"] interests but nothing else.

Is there a quick and simple way to do that? I found a SO answer but the explanation looks a bit vague to me.


#2

As you’ve surmised, there is not a built-in function for “replace all targets” of a given many-cardinality attribute.

The general approach to solving this problem is to use a transaction function (or a tx-fn Ion if you’re using Cloud).

A related post (also on SO) presents an additional description and approach: https://stackoverflow.com/questions/42112557/datomic-schema-for-a-to-many-relationship-with-a-reset-operation