# Conditional Upsert/Mutation using a scalar variable and Val function

**URL:** <https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979>\
**Category:** Dgraph\
**Tags:** mutation, kind:question, dgraph\
**Created:** [October 15, 2020, 5:00pm UTC](https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979 "2020-10-15T17:00:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vijay1](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/vijay1/32/6562_2.png) [@Vijay1](https://discuss.dgraph.io/u/Vijay1)\
**Post date:** [October 15, 2020, 5:00pm UTC](https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979/1 "2020-10-15T17:00:26Z")

</div>

Hi  
I am trying to use the query variable to update a scalar predicate (list of string type) from one node to another but it is not working. Both the “abc123” and “xyz123” nodes exist and the stringlist predicate for xyz123 is non-empty. It looks the val() function only works in the context of the uid(fromUid) but what is the correct way to update an existing node’s scalar predicate (list of string type) with value of the same predicate from a different node.

e.g. This is not working as intended - stringlist predicate is not updated in toUid node.

```auto
upsert {
    query {
    var(func: eq(someId, "abc123")) {
            toUid as uid
    }
    var(func: eq(someId, "xyz123")) {
      	fromUid as uid
      	h as stringlist
     }  
  }
  mutation @if(eq(len(toUid), 1) AND eq(len(fromUid), 1)) {
    set {
      uid(toUid) <stringlist> val(h) .
    }
  }
}

```

---

<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:** [October 15, 2020, 7:26pm UTC](https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979/2 "2020-10-15T19:26:34Z")

</div>

Actually, I got

> “message”: “: Value variables not supported for predicate with list type.”

So, val doesn’t support list type. That should be a request.

### Sample dataset

```auto
{
   "set": [
      {
         "someId": "abc123"
      },
      {
         "someId": "xyz123",
         "stringlist": [
            "Grape",
            "Apple",
            "Strawberry",
            "Banana",
            "watermelon"
         ]
      }
   ]
}

```

---

<div class="post-metadata">

**Author:** ![Vijay1](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/vijay1/32/6562_2.png) [@Vijay1](https://discuss.dgraph.io/u/Vijay1)\
**Post date:** [October 16, 2020, 12:12am UTC](https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979/3 "2020-10-16T00:12:08Z")

</div>

Thanks Michel for the reply

There are couple of issues I see here - both related to inconsistent behavior.

1. If the stringlist has one element then there is no “Value variables not supported for predicate with list type.” error but still silently fails.
2. If the predicate is an edge list or edge then it can be moved/set between nodes but if the predicate is a scalar type then there is no way to move then within upsert query afaik.

Is this something that can be fixed in the future releases?

---

<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:** [October 16, 2020, 12:33am UTC](https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979/4 "2020-10-16T00:33:26Z")

</div>

> [@Vijay1](#):
>
> If the stringlist has one element then there is no “Value variables not supported for predicate with list type.” error but still silently fails.

I think you are in a old version. I’m using 20.07.0 and it worked fine with a single value. But it should throw an error instead. As it is not supported.

> [@Vijay1](#):
>
> If the predicate is an edge list or edge then it can be moved/set between nodes

Yeah, relational edges works differently than a List type.

> [@Vijay1](#):
>
> but if the predicate is a scalar type then there is no way to move then within upsert query afaik.

Yeah, cuz you have to do like this

```auto
upsert {
    query {
    var(func: eq(someId, "abc1234")) {
            toUid as uid
    }
    var(func: eq(someId, "xyz1234")) {
      	fromUid as uid
      	h as stringlist
     }
      me(){
        newH as max(val(h))
      }
  }
  mutation @if(eq(len(toUid), 1) AND eq(len(fromUid), 1)) {
    set {
      uid(toUid) <stringlist> val(newH) .
    }
  }
}

```

I have an issue for this case but was decided for now to maintain this kind of pattern.

You can follow the whole story bellow

[https://github.com/dgraph-io/dgraph/issues/4779](https://github.com/dgraph-io/dgraph/issues/4779)

[https://github.com/dgraph-io/dgraph/issues/4712](https://github.com/dgraph-io/dgraph/issues/4712)

---

<div class="post-metadata">

**Author:** ![Vijay1](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/vijay1/32/6562_2.png) [@Vijay1](https://discuss.dgraph.io/u/Vijay1)\
**Post date:** [October 16, 2020, 1:29am UTC](https://discuss.dgraph.io/t/conditional-upsert-mutation-using-a-scalar-variable-and-val-function/10979/5 "2020-10-16T01:29:09Z")

</div>

I am using the dgraph/standalone:v20.07.1 image
