# Unique Identifer based on specified unique predicates

**URL:** <https://discuss.dgraph.io/t/unique-identifer-based-on-specified-unique-predicates/4602>\
**Category:** Dgraph\
**Tags:** mutation, example\
**Created:** [June 11, 2019, 11:43am UTC](https://discuss.dgraph.io/t/unique-identifer-based-on-specified-unique-predicates/4602 "2019-06-11T11:43:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![damienstamates](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@damienstamates](https://discuss.dgraph.io/u/damienstamates)\
**Post date:** [June 11, 2019, 11:43am UTC](https://discuss.dgraph.io/t/unique-identifer-based-on-specified-unique-predicates/4602/1 "2019-06-11T11:43:34Z")

</div>

Hello,

I’ve been playing around with the `@upsert` directive for Dgraph’s schema, and I’ve noticed that the transactions aren’t being aborted when I try adding the exact same node to Dgraph. In this example I mutate with the exact same RDF five times and it doesn’t return any errors.

I’m already running on the latest release of Dgraph and Upsert procedures won’t work for my project so I’m curious how I can go about this when figuring out if a node already exists in the graph. Any help would be appreciated.

Thanks

```auto
package main

import (
	"context"
	"log"

	"github.com/dgraph-io/dgo"
	"github.com/dgraph-io/dgo/protos/api"
	"google.golang.org/grpc"
)

const (
	schema = `
	name: string @index(hash) .
	ssn: string @index(hash) @upsert .
	policy: string @index(hash) @upsert .
	`
)

func main() {
	// Dial for Dgraph using grpc.
	conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("Failed when dialing grpc [%v]", err)
	}
	defer conn.Close()

	// Create a new Dgraph client for our mutations.
	dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))

	// Alter the schema to be equal to our schema variable.
	err = dg.Alter(context.Background(), &api.Operation{Schema: schema})
	if err != nil {
		log.Fatalf("Alteration error with setting schema [%v]\n", err)
	}

	for i := 0; i < 5; i++ {
		t := dg.NewTxn()

		// Running the same RDF through over and
		// over and none of them return errors.
		a, err := t.Mutate(context.Background(), &api.Mutation{SetNquads: []byte(`
		<8724427901147200807> <ssn> "126" .
		<12761900732112355942> <hasExact> <8724427901147200807> .
		<14815097164566158058> <policy> "JKL" .
		<12761900732112355942> <hasExact> <14815097164566158058> .
		`)})
		if err != nil {
			log.Fatalln("MUT:", err.Error())
		}

		t.Commit(context.Background())

		t.Discard(context.Background())

		log.Println(a.String() + "\n-------------")
	}
}

```

---

<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:** [June 11, 2019, 1:52pm UTC](https://discuss.dgraph.io/t/unique-identifer-based-on-specified-unique-predicates/4602/2 "2019-06-11T13:52:53Z")

</div>

> [@damienstamates](#):
>
> transactions aren’t being aborted when I try adding the exact same node to Dgraph.

upserts on Dgraph doesn’t work like that. You have upsert directive and upsert procedure. You need both. However we are finishing a new feature that may be useful to you.

> <https://github.com/dgraph-io/dgraph/issues/3059>
>
> This operation would allow a user to run a query, and use its results (via varia…bles) to execute the mutation. This would then allow upserts to happen, without doing multiple network calls to the DB.
> 
> \`\`\`
> txn {
> query {
> me(func: eq(email, "someone@gmail.com")) {
> v as uid
> }
> }
> 
> mutation @if(eq(len(v), 0)) {
> set {
> \<uid(v)\> \<name\> "Some One" .
> \<uid(v)\> \<email\> "someone@gmail.com" .
> }
> }
> }
> \`\`\`
> 
> This txn would check for an account with \`email=someone@gmail.com\`, and only if it is present, would
> it run the mutation.
> 
> This should return the result of query, normally. And also return the result of the mutation.
> 
> Additionally, we should be able to optionally do the mutation, iff the email is already present.
> \`\`\`
> txn {
> query {
> me(func: eq(email, "someone@gmail.com")) {
> v as uid
> }
> }
> 
> mutation @if(gt(len(v), 0)) {
> set {
> \<uid(v)\> \<name\> "Changed Name" .
> }
> }
> }
> \`\`\`
> This would introduce a new txn operator, and a new if directive applicable for mutations.

---

<div class="post-metadata">

**Author:** ![damienstamates](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@damienstamates](https://discuss.dgraph.io/u/damienstamates)\
**Post date:** [June 11, 2019, 7:13pm UTC](https://discuss.dgraph.io/t/unique-identifer-based-on-specified-unique-predicates/4602/3 "2019-06-11T19:13:33Z")

</div>

Okay, well is there an ETA on this new feature?

---

<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:** [June 11, 2019, 9:12pm UTC](https://discuss.dgraph.io/t/unique-identifer-based-on-specified-unique-predicates/4602/4 "2019-06-11T21:12:57Z")

</div>

Not sure, but maybe next month or a week earlier.
