# Upsert creates an empty node when query variable is empty

**URL:** <https://discuss.dgraph.io/t/upsert-creates-an-empty-node-when-query-variable-is-empty/12627>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [February 3, 2021, 9:35pm UTC](https://discuss.dgraph.io/t/upsert-creates-an-empty-node-when-query-variable-is-empty/12627 "2021-02-03T21:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pepo](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pepo/32/2074_2.png) [@Pepo](https://discuss.dgraph.io/u/Pepo)\
**Post date:** [February 3, 2021, 9:35pm UTC](https://discuss.dgraph.io/t/upsert-creates-an-empty-node-when-query-variable-is-empty/12627/1 "2021-02-03T21:35:56Z")

</div>

## I Want to Do

I want to query for a set of nodes, and then assign those nodes as the target of new edges.

## What I Did

I run an upsert like this:

```auto
upsert {
  query {
    p1 as var(func: eq(xid, "EXT_ID")) @recurse {  
      child as children
    }
  }

  mutation {
    set {
      uid(p1) <ecosystem> uid(child) .
    }
  }
}

```

However, even if the “EXT\_ID” don’t have any `children` (meaining the `child` variable is empty), a new edge `<ecosystem>` from `p1` to a **new empty node** is created.

This is, even if the query

```auto
{
  p1(func: eq(xid, "EXT_ID")) {  
    eco as children {
      uid
    }
  }
  eco(func: uid(eco)) {
    uid
  }
}

```

returns empty,

```auto
"data": {
    "p1": [],
    "eco": []
  },

```

the upsert shown above results in a new node being returned from the query

```auto
{
  perspectives(func: eq(xid, "EXT_ID")) {        
    ecosystem {
      uid
    }
  }
}

```

The result of the query before running the upsert:

```auto
 "perspectives": [
      {
        "ecosystem": [
          {
            "uid": "0x111704"
          },
          {
            "uid": "0x113e11"
          }
        ]
      }
    ]

```

and after running the upsert

```auto
 "perspectives": [
      {
        "ecosystem": [
          {
            "uid": "0x111704"
          },
          {
            "uid": "0x113e11"
          },
          {
            "uid": "0x113e12"
          }
        ]
      }
    ]

```

## Dgraph Metadata

docker image `dgraph/standalone:v20.03.3`  
ubuntu 20

---

<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:** [February 3, 2021, 10:27pm UTC](https://discuss.dgraph.io/t/upsert-creates-an-empty-node-when-query-variable-is-empty/12627/2 "2021-02-03T22:27:28Z")

</div>

> [@Pepo](#):
>
> a **new empty node** is created

That’s the upsert behave. If you wanna more control you should use [Conditional Upsert](https://dgraph.io/docs/mutations/conditional-upsert/).

---

<div class="post-metadata">

**Author:** ![Pepo](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/pepo/32/2074_2.png) [@Pepo](https://discuss.dgraph.io/u/Pepo)\
**Post date:** [February 4, 2021, 12:37pm UTC](https://discuss.dgraph.io/t/upsert-creates-an-empty-node-when-query-variable-is-empty/12627/3 "2021-02-04T12:37:49Z")

</div>

Thanks @MichelDiz 🙂
