# Problem while using uid in if condition in a conditional upsert operation

**URL:** https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419
**Category:** Dgraph
**Tags:** mutation
**Created:** [April 23, 2020, 3:08pm UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419 "2020-04-23T15:08:50Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![shravan](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@shravan](https://discuss.dgraph.io/u/shravan)
#### Post date: [April 23, 2020, 3:08pm UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/1 "2020-04-23T15:08:50Z")

</div>

```auto
upsert{
  query{
    var (func: eq(user.name, "Matt")){
       id as uid
    }
  }
  mutation @if(eq(val(id), 0x001)){
    set{
      uid(id) <user.job> "Engineer"
    }
  }
}

```

In this case, the if condition does not return true which will in turn execute the set operation. I know for the fact that user Matt’s uid is 0x001 and even when the var block is used separately as a query block to display Matt’s uid, it displays 0x001 but, the if condition does not return true.

**EDIT : changed the previous if condition from @if(val(id), 0x001) to @if(eq(val(id), 0x001)) which was the intended query**

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [April 23, 2020, 4:56pm UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/2 "2020-04-23T16:56:49Z")

</div>

This isn’t supported. Conditional upserts accepts length functions only, no other feature. Length is the recommended function for this case.

e.g:

```auto
upsert {
  query {
    ID as var(func: eq(user.name, "Matt"))
    check as var(func: uid(ID)) @filter(uid(0x001))
  }

  mutation @if(eq(len(check), 1) ) {
    set {
      uid(check) <user.job> "Engineer"
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![shravan](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@shravan](https://discuss.dgraph.io/u/shravan)
#### Post date: [April 23, 2020, 5:25pm UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/3 "2020-04-23T17:25:45Z")

</div>

But, this only ensures the existence of the uid but it wouldn’t make sure if check is equal to a uid that you might want to give as an operand to eq function. Am I right?

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [April 23, 2020, 7:04pm UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/4 "2020-04-23T19:04:16Z")

</div>

> [@shravan](#):
>
> it wouldn’t make sure if check is equal to a uid

The filter `@filter(uid(0x001))` guarantees to you that. So, if “Matt” uid isn’t equal to `0x001` it will fail and it will return a length of 0. If it is equal to `0x001` it will succeed and return a length of 1 and execute the mutation in the upsert block.

Actually, you don’t need the “check” block, you can do that in the first block. I did it this way to be visually didactic.

---

<div class="post-metadata">

### Author: ![shravan](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@shravan](https://discuss.dgraph.io/u/shravan)
#### Post date: [April 24, 2020, 3:14am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/5 "2020-04-24T03:14:54Z")

</div>

Sounds like a good workaround.

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [April 24, 2020, 3:26am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/6 "2020-04-24T03:26:46Z")

</div>

BTW see this issue - Really curious that the interest on this has increased in the last 24 hours.

> <https://github.com/dgraph-io/dgraph/issues/5281>
>
> \### What version of Dgraph are you using?
> dgraph:v20.03.0
> 
> \### Have you tried… reproducing the issue with the latest release?
> Already using latest version
> 
> \### What is the hardware spec (RAM, OS)?
> MAC OS, 32GB RAM
> 
> \### Steps to reproduce the issue (command/config used to run Dgraph).
> \`\`\`
> \# Insert Node A with timestamp
> {
> set {
> \_:nodeA \<dgraph.type\> "NodeA" .
> \_:nodeA \<nodeAId\> "sb1" .
> \_:nodeA \<timestamp\> "2020-04-20T22:15:19.794Z" .
> }
> }
> \# Query nodeA too make sure it exist, use same query as upsert to test that its working
> {
> var(func: eq(nodeAId, "sb1")) {
> existingTimestamp as timestamp
> }
> nodeA (func: le(val(existingTimestamp), "2020-04-20T22:15:19.794Z")) {
> uid
> timestamp
> }
> }
> 
> \# Run upsert on NodeB
> upsert {
> query {
> var(func: eq(nodeBId, "eb12")) {
> nodeBId as uid
> }
> 
> var(func: eq(nodeAId, "sb1")) {
> existingTimestamp as timestamp
> }
> }
> # create NodeB node if doesn't exist based on nodeA timestamp
> mutation @if(le(val(existingTimestamp), "2020-04-20T22:15:19.794Z")) {
> set {
> uid(nodeBId) \<createdAt\> "2020-04-23T00:41:48.497Z" .
> uid(nodeBId) \<dgraph.type\> "NodeB" .
> }
> }
> }
> 
> \`\`\`
> \### Expected behaviour and actual result.
> conditional \`upsert\` should work find on field other than \`uid\`. 
> Sample example given at following document is using \`uid\` only. In my case, I want to apply \`val\` on the predicate \`uid\` and then compare with corresponding value, but its not working. 
> https://dgraph.io/docs/master/mutations/#example-of-multiple-mutation-blocks

---

<div class="post-metadata">

### Author: ![shravan](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@shravan](https://discuss.dgraph.io/u/shravan)
#### Post date: [April 24, 2020, 3:43am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/7 "2020-04-24T03:43:50Z")

</div>

That sounds great, now we can put conditional upsert to extensive with the addition of other functions inside if.

---

<div class="post-metadata">

### Author: ![MIguel\_Angel\_Esteve](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/miguel_angel_esteve/32/6563_2.png) [@MIguel\_Angel\_Esteve](https://discuss.dgraph.io/u/MIguel_Angel_Esteve)
#### Post date: [May 3, 2021, 8:14am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/8 "2021-05-03T08:14:56Z")

</div>

Hi, I have a similar use case. Is it in the roadmap to add other different conditions in the conditional upsert like comparing with a value of a variable?

Although it can be solved in the way you indicate, I find it a bit strange having to make another query to have a length.

Thanks!

---

<div class="post-metadata">

### Author: ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)
#### Post date: [May 3, 2021, 12:22pm UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/9 "2021-05-03T12:22:14Z")

</div>

I don’t think this has priority. Let me ping @hardik.

---

<div class="post-metadata">

### Author: ![hardik](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/hardik/32/5053_2.png) [@hardik](https://discuss.dgraph.io/u/hardik)
#### Post date: [May 4, 2021, 4:51am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/10 "2021-05-04T04:51:22Z")

</div>

Added this to items to be considered for Roadmap 🙂.

@MIguel_Angel_Esteve and @shravan we will keep you posted. But it would take less priority as of now so we do recommend to use the solution suggested above.

---

<div class="post-metadata">

### Author: ![MIguel\_Angel\_Esteve](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/miguel_angel_esteve/32/6563_2.png) [@MIguel\_Angel\_Esteve](https://discuss.dgraph.io/u/MIguel_Angel_Esteve)
#### Post date: [May 7, 2021, 7:45am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/11 "2021-05-07T07:45:18Z")

</div>

Great! Yes, I’ll use the current solution in the meantime.  
Thank you very much, I think that can simplify the code to use if we follow that path.

---

<div class="post-metadata">

### Author: ![pratik\_awasarmol](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pratik_awasarmol/32/11560_2.png) [@pratik\_awasarmol](https://discuss.dgraph.io/u/pratik_awasarmol)
#### Post date: [February 15, 2024, 8:01am UTC](https://discuss.dgraph.io/t/problem-while-using-uid-in-if-condition-in-a-conditional-upsert-operation/6419/12 "2024-02-15T08:01:03Z")

</div>

can we use else here along with if
