# Upsert resulting duplicate node

**URL:** <https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710>\
**Category:** Dgraph\
**Tags:** dgraph\
**Created:** [January 29, 2022, 1:22am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710 "2022-01-29T01:22:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![porsche](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/porsche/32/9579_2.png) [@porsche](https://discuss.dgraph.io/u/porsche)\
**Post date:** [January 29, 2022, 1:22am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/1 "2022-01-29T01:22:43Z")

</div>

My below upsert statement always resulting in duplicate nodes.  
Any help.

```auto
 upsert {
  query {
   student_uid as var(func: eq(Student.studentId, "009aec0b-0068-16bb-e914-379de14d4e9a")) {uid}
  }

  mutation {
   set {
    uid(student_uid) <Student> <_:my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9a> .
    <_:my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9a> <dgraph.type> "Student" .
    <_:my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9a> <xid> "my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9" .
    <_:my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9a> <Student.studentId> "009aec0b-0068-16bb-e914-379de14d4e" .
   }
  }  
}

```

- My schema

```auto
type Student {
  studentId: String! @id
  courses: [Course] @hasInverse(field: student)
  xid: String! @search(by: [hash])
}

```

---

<div class="post-metadata">

**Author:** ![porsche](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/porsche/32/9579_2.png) [@porsche](https://discuss.dgraph.io/u/porsche)\
**Post date:** [January 29, 2022, 1:57am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/2 "2022-01-29T01:57:27Z")

</div>

Also, I tried this variant

```auto
 upsert{
  query {
   student_uid as var(func: eq(xid, "my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9")) {
    uid 
   }
  }

 mutation {
  set {
   uid(student_uid) <xid> "my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9" .
   uid(student_uid) <Student.studentId> "009aec0b-0068-16bb-e914-379de14d4e9" .
   uid(student_uid) <dgraph.type> "Student" .
  }
 }
}

```

---

<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:** [January 29, 2022, 2:32am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/3 "2022-01-29T02:32:20Z")

</div>

> [@porsche](#):
>
> `<_:my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9a>`

You are using Blank node in an upsert query. Blank node means “create a new node” and every time you hit mutate, it will create a new node. And link with the one you are targeting.

Can you explain what you are trying to do? Feels like you wanna upsert the Student entity, but why the `uid(student_uid) <Student> <_:my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9a> .` ?

> [@porsche](#):
>
> `uid(student_uid) <xid> "my.org/Student/009aec0b-0068-16bb-e914-379de14d4e9" .`

I don’t recommend this approach. If you are relying in the XID predicate. You should never try to upsert that value.

---

<div class="post-metadata">

**Author:** ![porsche](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/porsche/32/9579_2.png) [@porsche](https://discuss.dgraph.io/u/porsche)\
**Post date:** [January 29, 2022, 2:38am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/4 "2022-01-29T02:38:23Z")

</div>

- Also, I tried this variant

```auto
   upsert {
    query {
     var(func: eq(Student.studenttId, "009aec0b-0068-16bb-e914-379de14d4e9")) {
      student_uid as uid
    }
   }

   mutation {
    set {
      uid(student_uid ) <xid> "my.org/Patient/009aec0b-0068-16bb-e914-379de14d4e9" .
      uid(student_uid ) <Student.studenttId> "009aec0b-0068-16bb-e914-379de14d4e9" .
      uid(student_uid ) <dgraph.type> "Student" .
     }
    }  
   }

```

---

<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:** [January 29, 2022, 2:40am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/5 "2022-01-29T02:40:43Z")

</div>

> [@porsche](#):
>
> Also, I tried this variant

And what happened? This query seems valid.

---

<div class="post-metadata">

**Author:** ![porsche](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/porsche/32/9579_2.png) [@porsche](https://discuss.dgraph.io/u/porsche)\
**Post date:** [January 29, 2022, 2:41am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/6 "2022-01-29T02:41:18Z")

</div>

Duplicate node, interestingly both have same uid and studentId

```auto
{
 test2(func: has(Student.StudentId)) {
  Student.studentId
 }
}

```

Below reply

```auto
{
  "data": {
    "test2": [
      {
        "Student.studentId": "009aec0b-0068-16bb-e914-379de14d4e9"
      },
      {
        "Student.studentId": "009aec0b-0068-16bb-e914-379de14d4e9"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 76702,
      "processing_ns": 1287032,
      "encoding_ns": 22201,
      "assign_timestamp_ns": 677617,
      "total_ns": 2125653
    },
    "txn": {
      "start_ts": 1044
    },
    "metrics": {
      "num_uids": {
        "Student.studentId": 2,
        "_total": 2
      }
    }
  }
}

```

---

<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:** [January 29, 2022, 2:43am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/7 "2022-01-29T02:43:08Z")

</div>

Not possible, make sure to delete all the other duplicated and try again. The query is valid.

---

<div class="post-metadata">

**Author:** ![porsche](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/porsche/32/9579_2.png) [@porsche](https://discuss.dgraph.io/u/porsche)\
**Post date:** [January 29, 2022, 2:52am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/8 "2022-01-29T02:52:23Z")

</div>

- It’s a consistent respro for me.
- I did DROP DATA and re-executed mutations and query
- Query

```auto
{
 test2(func: has(Student.studentId)) {
  Student.studentId
  uid
 }
}

```

- Mutation

```auto
 upsert{
  query {
   var(func: eq(Student.studentId, "009aec0b-0068-16bb-e914-379de14d4e9")) {
   student_uid as uid
  }
 }
 mutation {
  set {
    uid(student_uid ) <Student.studentId> "009aec0b-0068-16bb-e914-379de14d4e9" .
    uid(student_uid ) <dgraph.type> "Patient" .
   }
  }  
}

```

- Reply

```auto
{
"data": {
  "test2": [
    {
      "Student.studentId": "009aec0b-0068-16bb-e914-379de14d4e9",
      "uid": "0x4c2"
    },
    {
      "Student.studentId": "009aec0b-0068-16bb-e914-379de14d4e9",
      "uid": "0x4c3"
    }
  ]
},
"extensions": {
  "server_latency": {
    "parsing_ns": 56601,
    "processing_ns": 917524,
    "encoding_ns": 24700,
    "assign_timestamp_ns": 590615,
    "total_ns": 1633441
  },
  "txn": {
    "start_ts": 1094
  },
  "metrics": {
    "num_uids": {
      "Student.studentId": 2,
      "_total": 4,
      "uid": 2
    }
  }
}
}

```

---

<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:** [January 29, 2022, 3:19am UTC](https://discuss.dgraph.io/t/upsert-resulting-duplicate-node/16710/9 "2022-01-29T03:19:26Z")

</div>

Porsche, is that your name?

Look, computers don’t lie. I see your query, and I see it is valid. There’s no other indication that would lead to duplication. Dgraph itself has a ton of self testing steps before shipping any feature. And it is not possible to have duplicate if your query is valid. Which means, you haven’t dropped the data correctly.

And why this UUID 009aec0b-0068-16bb-e914-379de14d4e9 is a student in the previous results and now it is a Patient?

In order to delete something you have to have a perfect aligned Schema and also the correct `<dgraph.type>` value. If something is wrong in the middle, it won’t delete the data.

So, please, instead of trying Drop or something. Delete all data from Dgraph manually. Deleting the whole files in the p, zw, w directories. With this you gonna start from scratch.

BTW. If you gonna rely on XID you should read this [https://dgraph.io/docs/mutations/external-ids-upsert-block/#sidebar](https://dgraph.io/docs/mutations/external-ids-upsert-block/#sidebar)

Cheers!
