# Dgraph auto increment id with DQL

**URL:** https://discuss.dgraph.io/t/dgraph-auto-increment-id-with-dql/19708
**Category:** Dgraph
**Tags:** dql
**Created:** [January 13, 2025, 4:38am UTC](https://discuss.dgraph.io/t/dgraph-auto-increment-id-with-dql/19708 "2025-01-13T04:38:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![himanshu-msphere](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/himanshu-msphere/32/11744_2.png) [@himanshu-msphere](https://discuss.dgraph.io/u/himanshu-msphere)
#### Post date: [January 13, 2025, 4:38am UTC](https://discuss.dgraph.io/t/dgraph-auto-increment-id-with-dql/19708/1 "2025-01-13T04:38:14Z")

</div>

Use below DQL for set auto increment code for database

```auto
upsert {
  query {
    var(func: has(User.code), orderdesc: User.code, first:1) {
      lastId as User.code
    }
    me() {
      VD as sum(val(lastId))
      Increment as math(VD + 1)
    }
  }
  mutation {
    set {
      _:user <dgraph.type> "User" .
      _:user <User.code> val(Increment) .
      _:user <User.name> "John" .
    }
  }
}

```

Which is working fine but when there is no record on `User` (first time) shown error as `while running ToJson: Only aggregated variables allowed within empty block.`

---

<div class="post-metadata">

### Author: ![matthewmcneely](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthewmcneely/32/4774_2.png) [@matthewmcneely](https://discuss.dgraph.io/u/matthewmcneely)
#### Post date: [January 13, 2025, 3:54pm UTC](https://discuss.dgraph.io/t/dgraph-auto-increment-id-with-dql/19708/2 "2025-01-13T15:54:58Z")

</div>

Check out the @if directive. Very common use case that it solves.

> [@Conditional Upsert - Mutations](https://discuss.hypermode.com/t/conditional-upsert-mutations/9842):
>
> The upsert block also allows specifying conditional mutation blocks using an @if directive. The mutation is executed only when the specified condition is true. If the condition is false, the mutation is silently ignored. The general structure of Conditional Upsert looks like as follows: upsert { query \<query block\> [fragment \<fragment block\>] mutation [@if(\<condition\>)] \<mutation block 1\> [mutation [@if(\<condition\>)] \<mutation block 2\>] ... } The @if directive accepts a condition on…

---

<div class="post-metadata">

### Author: ![himanshu-msphere](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/himanshu-msphere/32/11744_2.png) [@himanshu-msphere](https://discuss.dgraph.io/u/himanshu-msphere)
#### Post date: [January 16, 2025, 9:07am UTC](https://discuss.dgraph.io/t/dgraph-auto-increment-id-with-dql/19708/3 "2025-01-16T09:07:58Z")

</div>

Here @if condition allow on mutation.  
Error because of `Increment as math(VD + 1)` line where can’t use if condition. also condition can’t use in set value.  
On single mutation need to add 1 for code if `Increment` not found.

---

<div class="post-metadata">

### Author: ![himanshu-msphere](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/himanshu-msphere/32/11744_2.png) [@himanshu-msphere](https://discuss.dgraph.io/u/himanshu-msphere)
#### Post date: [January 22, 2025, 8:39am UTC](https://discuss.dgraph.io/t/dgraph-auto-increment-id-with-dql/19708/4 "2025-01-22T08:39:45Z")

</div>

```auto
upsert {
  query {
    var(func: has(User.code), orderdesc: User.code, first:1) {
      lastId as User.code
      VD as math(lastId + 1)
    }
    me() {
      Increment as sum(val(VD))
    }
  }
  mutation {
    set {
      _:user <dgraph.type> "User" .
      _:user <User.name> "John" .
    }
  }
  mutation @if(gt(len(VD), 0)) {
    set { 
      _:user <User.code> val(Increment) . 
    }
  }
  mutation @if(eq(len(VD), 0)) {
    set {
      _:user <Repair.code> "1" .
    }
  }
}

```
