# Replace cardinality-many attribute

**URL:** <https://forum.datomic.com/t/replace-cardinality-many-attribute/759>\
**Category:** General\
**Created:** [December 23, 2018, 8:45am UTC](https://forum.datomic.com/t/replace-cardinality-many-attribute/759 "2018-12-23T08:45:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![igrishaev](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/igrishaev/32/244_2.png) [@igrishaev](https://forum.datomic.com/u/igrishaev)\
**Post date:** [December 23, 2018, 8:45am UTC](https://forum.datomic.com/t/replace-cardinality-many-attribute/759/1 "2018-12-23T08:45:23Z")

</div>

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

```auto
{: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.:

```auto
{: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.

> <https://stackoverflow.com/questions/42786046/how-to-update-overwrite-a-ref-attribute-with-cardinality-many-in-datomic>

---

<div class="post-metadata">

**Author:** ![marshall](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/marshall/32/48_2.png) [@marshall](https://forum.datomic.com/u/marshall)\
**Post date:** [December 26, 2018, 7:03pm UTC](https://forum.datomic.com/t/replace-cardinality-many-attribute/759/2 "2018-12-26T19:03:26Z")

</div>

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](https://stackoverflow.com/questions/42112557/datomic-schema-for-a-to-many-relationship-with-a-reset-operation)
