# Update one-to-many relationship

**URL:** <https://discuss.dgraph.io/t/update-one-to-many-relationship/7429>\
**Category:** Dgraph\
**Created:** [June 30, 2020, 7:39am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429 "2020-06-30T07:39:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![poketulhu](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/poketulhu/32/3047_2.png) [@poketulhu](https://discuss.dgraph.io/u/poketulhu)\
**Post date:** [June 30, 2020, 7:39am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/1 "2020-06-30T07:39:20Z")

</div>

Hi,  
I have the schema

```auto
uuid: string @index(hash) .
owner: uid @reverse .

type Owner {
    uuid: string
    <~owner>
}

type Device {
    uuid: string
    owner: Owner
}

```

The owner has many devices, the device has one owner

And the next data

```auto
_:foo <uuid> "foo" .
_:foo <dgraph.type> "Owner" .

_:bar <uuid> "bar" .
_:bar <dgraph.type> "Owner" .

_:baz <uuid> "baz" .
_:baz <dgraph.type> "Device" .
_:baz <owner> _:foo .

```

I want to change the device owner - from foo to bar, so I run the upsert

```auto
query {
  device as var(func: eq(uuid, "baz")
  owner as var(func: eq(uuid, "bar")
}

mutation {
  set {
    uid(device) <owner> uid(owner) .
  }
}

```

The question is if it is right to make upsert in this way? Because after this upsert relation between baz-device and foo-owner still exists  
Or should I delete old relation and only then add a new one?

---

<div class="post-metadata">

**Author:** ![ahsan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ahsan/32/3517_2.png) [@ahsan](https://discuss.dgraph.io/u/ahsan)\
**Post date:** [June 30, 2020, 7:51am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/2 "2020-06-30T07:51:21Z")

</div>

Hi @poketulhu,

The mutation that you are doing is adding a new relation independent of the older relations. If you want to remove some relation then you should delete it as well.

---

<div class="post-metadata">

**Author:** ![poketulhu](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/poketulhu/32/3047_2.png) [@poketulhu](https://discuss.dgraph.io/u/poketulhu)\
**Post date:** [June 30, 2020, 8:02am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/3 "2020-06-30T08:02:14Z")

</div>

Then I have another problem: if I delete and set in the same mutation and old and new owners are the same, then delete overwrites set. So I lose relation between device and owner.  
What to do in this case?

---

<div class="post-metadata">

**Author:** ![ppp225](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ppp225/32/2007_2.png) [@ppp225](https://discuss.dgraph.io/u/ppp225)\
**Post date:** [June 30, 2020, 8:15am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/4 "2020-06-30T08:15:50Z")

</div>

I think it is related to this bug, that ~reverse doesn’t update correctly: [Reverse index doesn't update for non-list uid predicates · Issue #5732 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/issues/5732)

---

<div class="post-metadata">

**Author:** ![minhaj](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/minhaj/32/3468_2.png) [@minhaj](https://discuss.dgraph.io/u/minhaj)\
**Post date:** [June 30, 2020, 11:32am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/5 "2020-06-30T11:32:29Z")

</div>

Hey @poketulhu, your mutation looks good to me. Ideally, your mutation should set the `<owner>` to the new owner and delete the reverse edge for the previous owner to the device. There is a bug that @reverse edge is not being updated correctly which @ppp225 has pointed out.

---

<div class="post-metadata">

**Author:** ![poketulhu](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/poketulhu/32/3047_2.png) [@poketulhu](https://discuss.dgraph.io/u/poketulhu)\
**Post date:** [June 30, 2020, 12:01pm UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/7 "2020-06-30T12:01:01Z")

</div>

So there’s no need to delete before the update? Is the estimated deadline for fixing this bug known?

---

<div class="post-metadata">

**Author:** ![minhaj](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/minhaj/32/3468_2.png) [@minhaj](https://discuss.dgraph.io/u/minhaj)\
**Post date:** [June 30, 2020, 2:22pm UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/8 "2020-06-30T14:22:20Z")

</div>

> [@poketulhu](#):
>
> So there’s no need to delete before the update?

Yes,there is no need to delete in your case.

> [@poketulhu](#):
>
> Is the estimated deadline for fixing this bug known?

Our team will try to pick up this ASAP.

---

<div class="post-metadata">

**Author:** ![poketulhu](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/poketulhu/32/3047_2.png) [@poketulhu](https://discuss.dgraph.io/u/poketulhu)\
**Post date:** [July 1, 2020, 7:26am UTC](https://discuss.dgraph.io/t/update-one-to-many-relationship/7429/9 "2020-07-01T07:26:20Z")

</div>

Many thanks for the help!
