# Using an upsert block to increment an ID

**URL:** https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889
**Category:** Users
**Created:** [January 26, 2020, 7:26pm UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889 "2020-01-26T19:26:43Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Fantastitech](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/fantastitech/32/1504_2.png) [@Fantastitech](https://discuss.dgraph.io/u/Fantastitech)
#### Post date: [January 26, 2020, 7:26pm UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/1 "2020-01-26T19:26:43Z")

</div>

I’m trying to use an upsert block to increment an ID value on creation so I can have my own internal node IDs that are only unique to other nodes of the same type. I’m basing this on questions like this one which includes the same solution I came up with but it’s not working for me: [How to use Dgraph to automatically increase ID?](http://discuss.hypermode.com/t/how-to-use-dgraph-to-automatically-increase-id/5406)

I have the following manually-created users with my own xid field:

Query:

```auto
{
  var(func: type(User), first:-1) {
    lastid as xid
    newid as math(lastid + 1)
    }
  q(func: type(User)) {
    xid
    FirstName
    val(newid)
  }
}

```

Result:

```auto
"q": [
      {
        "xid": 1,
        "FirstName": "Chris"
      },
      {
        "xid": 2,
        "FirstName": "Phil",
        "val(newid)": 3
      }
    ]
  }

```

I want the value of “newid” which in this case is “3” to be able to be used in the mutation block of an upsert block like this:

```auto
upsert {
  query {
    q(func: type(User), first:-1) {
    lastid as xid
    newid as math(lastid + 1)
    }}

  mutation {
    set {
	_:user <dgraph.type> "User" .
      _:user <FirstName> "Matt" .
      _:user <LastName> "None" .
      _:user <Email> "matt@example.com" .
      _:user <Password> "password" .
      _:user <xid> val(newid) .
    }
  }
}

```

The above upsert mutation works but the xid is never set which I assume means it’s empty when executing the mutation block. How do I get my newid value into the mutation block where I need it?

The first query which returns my users seems to suggest the value only exists in the context of a specific node since the newid value isn’t shown on the “Chris” node which is why it seems to be empty during my mutation.

---

<div class="post-metadata">

### Author: ![amanmangal](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amanmangal/32/1249_2.png) [@amanmangal](https://discuss.dgraph.io/u/amanmangal)
#### Post date: [January 27, 2020, 11:57am UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/2 "2020-01-27T11:57:51Z")

</div>

I think relying on sorting of UIDs to get the latest inserted user would be a bad approach. We assign UIDs in batches and a later inserted user could potentially have lower UID. A better approach may be to keep a separate predicate and store the latest ingested XID there, like a counter and use that instead. This can have some negative impact on performance, though, happy to explain more if you have any question with the approach.

---

<div class="post-metadata">

### Author: ![Fantastitech](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/fantastitech/32/1504_2.png) [@Fantastitech](https://discuss.dgraph.io/u/Fantastitech)
#### Post date: [January 27, 2020, 3:42pm UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/3 "2020-01-27T15:42:33Z")

</div>

I’m not relying on the UIDs. That’s just an example from the linked post. I’m actually using `orderdesc: xid, first:1` to get the last saved xid. My problem is the variable not being recognized in the mutation block which wouldn’t be solved by changing how it’s stored.

---

<div class="post-metadata">

### Author: ![amanmangal](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amanmangal/32/1249_2.png) [@amanmangal](https://discuss.dgraph.io/u/amanmangal)
#### Post date: [January 28, 2020, 8:07am UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/4 "2020-01-28T08:07:42Z")

</div>

Will you share the actual query? In the above query, I see you are using `first:-1` which I thought could be problametic.

---

<div class="post-metadata">

### Author: ![Fantastitech](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/fantastitech/32/1504_2.png) [@Fantastitech](https://discuss.dgraph.io/u/Fantastitech)
#### Post date: [February 2, 2020, 3:51am UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/5 "2020-02-02T03:51:24Z")

</div>

```auto
upsert {
  query {
    var(func: type(User), orderdesc: xid, first:1) {
    lastid as xid
    newid as math(lastid + 1)
    }}

  mutation {
    set {
      _:user <dgraph.type> "User" .
      _:user <FirstName> "Matt" .
      _:user <LastName> "None" .
      _:user <Email> "matt@example.com" .
      _:user <Password> "password" .
      _:user <xid> val(newid) .
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![amanmangal](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amanmangal/32/1249_2.png) [@amanmangal](https://discuss.dgraph.io/u/amanmangal)
#### Post date: [February 12, 2020, 6:12am UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/6 "2020-02-12T06:12:27Z")

</div>

This seems like a bug similar to already raised on GitHub here [Bug with multi-level Upsert Block · Issue #4692 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/issues/4692). I am looking into this issue. The problem is the line `_:user <xid> val(newid) .`. I will post more updates soon. Thanks

---

<div class="post-metadata">

### Author: ![amanmangal](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amanmangal/32/1249_2.png) [@amanmangal](https://discuss.dgraph.io/u/amanmangal)
#### Post date: [February 12, 2020, 9:34am UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/7 "2020-02-12T09:34:10Z")

</div>

This is a similar bug, and we will fix it as soon as we can. I have posted an example on the GitHub issue [Bug with multi-level Upsert Block · Issue #4692 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/issues/4692).

---

<div class="post-metadata">

### Author: ![amanmangal](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amanmangal/32/1249_2.png) [@amanmangal](https://discuss.dgraph.io/u/amanmangal)
#### Post date: [February 12, 2020, 4:15pm UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/8 "2020-02-12T16:15:22Z")

</div>

@MichelDiz pointed out that there is a separate GitHub issue that tracks this issue [It is not possible to create a new node from another using Upsert Block · Issue #4712 · dgraph-io/dgraph · GitHub](https://github.com/dgraph-io/dgraph/issues/4712).

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)
#### Post date: [March 13, 2020, 4:19pm UTC](https://discuss.dgraph.io/t/using-an-upsert-block-to-increment-an-id/5889/9 "2020-03-13T16:19:55Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
