# Getting result from an upsert

**URL:** <https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239>\
**Category:** Dgraph Clients\
**Tags:** untagged, pydgraph\
**Created:** [December 2, 2019, 2:32pm UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239 "2019-12-02T14:32:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 2, 2019, 2:32pm UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/1 "2019-12-02T14:32:24Z")

</div>

**Moved from GitHub [pydgraph/102](https://github.com/dgraph-io/pydgraph/issues/102)**

_Posted by_ [robin-snt](https://github.com/robin-snt):

When doing an upsert, it is not clear how to get the result of the upsert.

E.g:

```python
def upsert(client, name, state):
    q = """query q($name: string)
    {
        NODE as q(func: eq(name, $name)) {
            expand(_all_)
        }
    }"""

    txn = client.txn()
    state['uid'] = 'uid(NODE)'
    m = txn.create_mutation(set_obj=state, cond='@if(eq(len(NODE), 1))')
    request = txn.create_request(query=q,
                                 mutations=(m,),
                                 variables={'$name': name},
                                 commit_now=True)

    try:
        result = txn.do_request(request)
    finally:
        txn.discard()

```

It would seem intuitive to have the newly changed state accessible in `result.json`, but reading this attribute returns `b''`.

What’s the intended way of getting the newly updated state? Make a new query? That would seem redundant to me, but perhaps there are good reasons to have the user explicitly make a new query.

Upon further contemplation, you could simply return `state` as that should be your updated data, but then it is unclear how to idiomatically check if the request caused a write to the db.

**EDIT** : Updated example with setting `uid`, but the issue still persists.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 4, 2019, 7:54pm UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/2 "2019-12-04T19:54:44Z")

</div>

[martinmr](https://github.com/martinmr) _commented_ :

You can’t change uids so I suggest using another field in your example.

I am not very familiar with upsert but I’ll try to answer. I’ll make sure @mangalaman93 takes a look in case I miss something.

Taken from the example in [https://docs.dgraph.io/mutations/#upsert-block](https://docs.dgraph.io/mutations/#upsert-block), an upsert mutation will have a field called `uids` that contains the uids of the nodes that changed in your query.

If the transaction is committed successfully and the uid of the node you are interested is in this field, then you know that at the point of the commit the state of the node is whatever you set it to in the transaction. The state of the node itself is not returned as that could potentially be a lot of data.

If the uid of the node you were trying to change is not in this field, then the node was not touched so you would need to run a query to get its current state.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 5, 2019, 8:44am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/3 "2019-12-05T08:44:05Z")

</div>

[robin-snt](https://github.com/robin-snt) _commented_ :

I thought I had already tried this method, but I will try again. Thanks for your input @martinmr .

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 6, 2019, 7:50am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/4 "2019-12-06T07:50:32Z")

</div>

[mangalaman93](https://github.com/mangalaman93) _commented_ :

We currently do not return the new state of the database after the upsert. This requires committing the transaction, and sending another query to the database to get the updated result. The reason we do this is, the result of query would be different depending upon whether the mutation/upsert was committed.

We recently made a change that is available in the master branch in the PR [Remove duplicate code for query processing by mangalaman93 · Pull Request #4269 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/pull/4269), that returns the result of the query **before** the mutation was executed.

I think @martinmr is referring to the PR [https://github.com/dgraph-io/dgraph/pull/4145](https://github.com/dgraph-io/dgraph/pull/4145). This change was removed in this PR [Remove duplicate code for query processing by mangalaman93 · Pull Request #4269 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/pull/4269).

You could access the latest docs at [https://docs.dgraph.io/master/mutations/#upsert-block](https://docs.dgraph.io/master/mutations/#upsert-block)

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 6, 2019, 8:07am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/5 "2019-12-06T08:07:01Z")

</div>

[robin-snt](https://github.com/robin-snt) _commented_ :

@mangalaman93 So currently there is no way of knowing if the upsert caused a write, apart from making a new query?

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 6, 2019, 8:09am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/6 "2019-12-06T08:09:58Z")

</div>

[mangalaman93](https://github.com/mangalaman93) _commented_ :

Correct. You could get some sense from the result of the query on previous state of the database. But the full proof approach would be to just query after committing the upsert/mutation.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 6, 2019, 8:12am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/7 "2019-12-06T08:12:24Z")

</div>

[robin-snt](https://github.com/robin-snt) _commented_ :

Thank you so much for your reply and help. Please state this explicitly in documentation.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [December 6, 2019, 9:17am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/8 "2019-12-06T09:17:34Z")

</div>

[mangalaman93](https://github.com/mangalaman93) _commented_ :

We do mention it explicitly in the documentation of the master branch.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [July 11, 2020, 4:29am UTC](https://discuss.dgraph.io/t/getting-result-from-an-upsert/8239/9 "2020-07-11T04:29:30Z")

</div>


