Cannot delete node by json

Moved from GitHub dgo/129

Posted by linuxr:

Hi, I could not delete node by uid, could someone help me.

My code is :

type Person struct {
        Uid     string   `json:"uid"`
        Name    string   `json:"name"`
        Age     int      `json:"age"`
        Friends []Person `json:"friends"`
}

func main() {
        c := NewDgraphClient()
        err := CreateSchema(c)
        if err != nil {
                log.Fatal(err)
        }
        err = AddSomeData(c)
        if err != nil {
                log.Fatal(err)
        }
        err = DeleteData(c)
        if err != nil {
                log.Fatal(err)
        }

}

func NewDgraphClient() *dgo.Dgraph {
        conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
        if err != nil {
                log.Fatal(err)
        }

        client := dgo.NewDgraphClient(api.NewDgraphClient(conn))

        return client
}

func CreateSchema(client *dgo.Dgraph) error {
        schema := `
name: string @index(term) .
age: int .
friends: [uid] .

type Person {
  name: string
  age: int
  friends: [Person]
}
`
        op := &api.Operation{Schema: schema}

        err := client.Alter(context.Background(), op)
        return err
}

func AddSomeData(client *dgo.Dgraph) error {
        p1 := &Person{
                Uid:  "_:dog",
                Name: "Dog",
                Age:  10,
        }
        p1.Friends = make([]Person, 0)

        p2 := &Person{
                Uid:  "_:monkey",
                Name: "Monkey",
                Age:  20,
        }
        p3 := &Person{
                Uid:  "_:cat",
                Name: "Cat",
                Age:  30,
        }

        p1.Friends = append(p1.Friends, *p2)
        p1.Friends = append(p1.Friends, *p3)

        mu := &api.Mutation{CommitNow: true}
        pb, err := json.Marshal(p1)
        if err != nil {
                return err
        }

        mu.SetJson = pb
        _, err = client.NewTxn().Mutate(context.Background(), mu)
        return err
}

func DeleteData(client *dgo.Dgraph) error {
        d := map[string]string{"uid": "0xfffd8d67d832b97d"}
        pb, err := json.Marshal(d)
        if err != nil {
                return err
        }
        fmt.Println(string(pb))
        mu := &api.Mutation{
                CommitNow:  true,
                DeleteJson: pb,
        }

        _, err = client.NewTxn().Mutate(context.Background(), mu)
        return err
}

DeleteData had not delete any data.
0xfffd8d67d832b97d is uid of the data created by *AddSomeData.
I find this uid by http://localhost:8000

AugustDev commented :

I haven’t run the code, but your node struct is not properly implemented and is lacking DType. You should redefine your struct and insert DType to every instance of the struct.

type Person struct {
        Uid     string   `json:"uid"`
        DType  []string           `json:"dgraph.type,omitempty"`
        Name    string   `json:"name"`
        Age     int      `json:"age"`
        Friends []Person `json:"friends"`
}

p1 := &Person{
        DType: []string{"Person"},
	Uid:  "_:dog",
	Name: "Dog",
	Age:  10,
}

NamanJain8 commented :

As @AugustDev suggested, @linuxr for deleting a node you need to have a well-defined type system and your node has predicate <dgraph.type>. ref: node-exists-after-deleting