Unique Identifer based on specified unique predicates

example
mutation

(Damien Stamates) #1

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

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-------------")
	}
}

(Michel Conrado) #2

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.


(Damien Stamates) #3

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


(Michel Conrado) #4

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