Moved from GitHub dgo/72
Posted by drwpeng:
There is a map[string]interface{} in structs. If I add “data: geo .” to schema:
- if there is no data in the dgraph:
This will not be able to save data and no error message - if there is data in the dgraph:
rpc error: code = unknown desc = Schema change not allowed from scalar to uid or vice versa while there is data for pred: data
My test case as follows:
package main
import (
"context"
"encoding/json"
"github.com/fatih/structs"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
"log"
)
type Animal struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Info string `json:"info,omitempty"`
Size string `json:"size,omitempty"`
}
type PResult struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Category string `json:"category,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}
func main() {
conn, _ := grpc.Dial("localhost:9080", grpc.WithInsecure())
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ani := Animal{
Id: "12345",
Name: "Tom",
Info: "dog",
Size: "10kg",
}
pr := PResult{
ID: "f11111",
Name: "animal",
Category: "friend",
Data: structs.Map(ani),
}
op := &api.Operation{}
op.Schema = `
id: string @index(exact) .
name: string .
category: string .
data: geo .
`
ctx := context.Background()
if err := dg.Alter(ctx, op); err != nil {
log.Fatal(err)
}
mu := &api.Mutation{
CommitNow: true,
}
pb, _ := json.Marshal(pr)
mu.SetJson = pb
_, _ = dg.NewTxn().Mutate(ctx, mu)
// fmt.Println(assign.Uids["blank-0"])
}