# Converting update query to an upsert query

**URL:** <https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626>\
**Category:** GraphQL\
**Tags:** kind:feature\
**Created:** [July 18, 2020, 4:56am UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626 "2020-07-18T04:56:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrjn](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrjn/32/775_2.png) [@mrjn](https://discuss.dgraph.io/u/mrjn)\
**Post date:** [July 18, 2020, 4:56am UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/1 "2020-07-18T04:56:06Z")

</div>

I have a type which has an ID. When I get the updates for that type, I don’t know if it is already present in the DB or not. I just want to add it if not present, and update it if present.

But, currently, I’ll have to try add first, then fail, then run update with a filter for that ID. That’s unnecessary work.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [July 19, 2020, 4:43am UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/2 "2020-07-19T04:43:55Z")

</div>

> [@mrjn](#):
>
> When I get the updates for that type, I don’t know if it is already present in the DB or not.

If you had an dgraph ID wouldn’t you know then that it already existed? I might be missing something here though…

---

<div class="post-metadata">

**Author:** ![mrjn](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrjn/32/775_2.png) [@mrjn](https://discuss.dgraph.io/u/mrjn)\
**Post date:** [July 19, 2020, 6:11pm UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/3 "2020-07-19T18:11:13Z")

</div>

It’s not a Dgraph ID. It is an external ID, coming from an external system, which is being internally mapped to a Dgraph ID.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [July 19, 2020, 8:48pm UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/4 "2020-07-19T20:48:25Z")

</div>

Ah, that makes more sense then. Sort of then needing a SQL syntax of INSERT OR UPDATE. The trick here is matching the external ID to a unique key in Dgraph if not using the ID type (my incoming data has a guid formatted id). But Dgraph has no way to force uniqueness on any type other than ID.

---

<div class="post-metadata">

**Author:** ![pawan](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pawan/32/1946_2.png) [@pawan](https://discuss.dgraph.io/u/pawan)\
**Post date:** [July 20, 2020, 6:45am UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/5 "2020-07-20T06:45:15Z")

</div>

> [@mrjn](#):
>
> I just want to add it if not present, and update it if present.

I can see how an upsert like this would be useful. Currently, updates check that the node should already exist whereas add only adds it if it doesn’t exist. We could possible provide an optional argument in the `updateType` mutation to achieve this behavior which would also create the node if it doesn’t exist.

@JatinDevDG could you please note this down in the Community requests for this week so that we could discuss this along with other user issues?

---

<div class="post-metadata">

**Author:** ![JatinDevDG](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jatindevdg/32/3417_2.png) [@JatinDevDG](https://discuss.dgraph.io/u/JatinDevDG)\
**Post date:** [July 20, 2020, 6:48am UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/6 "2020-07-20T06:48:09Z")

</div>

sure.

---

<div class="post-metadata">

**Author:** ![JatinDevDG](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jatindevdg/32/3417_2.png) [@JatinDevDG](https://discuss.dgraph.io/u/JatinDevDG)\
**Post date:** [July 27, 2020, 6:10am UTC](https://discuss.dgraph.io/t/converting-update-query-to-an-upsert-query/8626/7 "2020-07-27T06:10:03Z")

</div>

@mrjn Can you please post the schema and the steps that you tried? That will clear it a bit more and help us to look into it in more details.
