{"record":{"id":"833d39010ffe7461","repo":"dgraph-io/dgraph","slug":"invalid-uid-with-value-0","errorCode":null,"errorMessage":"invalid UID with value 0","messagePattern":"invalid UID with value 0","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"posting/index.go","lineNumber":100,"sourceCode":"// TODO - See if we need to pass op as argument as t should already have Op.\n\nfunc (txn *Txn) addIndexMutations(ctx context.Context, info *indexMutationInfo) ([]*pb.DirectedEdge, error) {\n\tif info.tokenizers == nil {\n\t\tinfo.tokenizers = schema.State().Tokenizer(ctx, info.edge.Attr)\n\t}\n\n\tif info.factorySpecs == nil {\n\t\tspecs, err := schema.State().FactoryCreateSpec(ctx, info.edge.Attr)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tinfo.factorySpecs = specs\n\t}\n\n\tattr := info.edge.Attr\n\tuid := info.edge.Entity\n\tif uid == 0 {\n\t\treturn []*pb.DirectedEdge{}, errors.New(\"invalid UID with value 0\")\n\t}\n\n\tif len(info.factorySpecs) > 0 {\n\t\tinKey := x.DataKey(info.edge.Attr, uid)\n\t\tpl, err := txn.Get(inKey)\n\t\tif err != nil {\n\t\t\treturn []*pb.DirectedEdge{}, err\n\t\t}\n\t\tdata, err := pl.AllValues(txn.StartTs)\n\t\tif err != nil {\n\t\t\treturn []*pb.DirectedEdge{}, err\n\t\t}\n\n\t\tif info.op == pb.DirectedEdge_DEL &&\n\t\t\tlen(data) > 0 && data[0].Tid == types.VFloatID {\n\t\t\t// TODO look into better alternatives\n\t\t\t//      The issue here is that we will create dead nodes in the Vector Index\n\t\t\t//      assuming an HNSW index type. What we should do instead is invoke","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/posting/index.go#L82-L118","documentation":"addIndexMutations validates that the incoming DirectedEdge has a non-zero Entity (source UID) before performing any index writes. A UID of 0 is meaningless in Dgraph (UIDs start at 1), so an edge with Entity == 0 cannot be indexed and the whole mutation is rejected with 'invalid UID with value 0'.","triggerScenarios":"Calling AddMutationWithIndex (or the internal anonymous caller) with a DirectedEdge whose Entity field is 0 — typically an edge constructed without setting Entity, or a JSON/RDF mutation referencing the special '*' or a blank-node that failed UID assignment.","commonSituations":"Hand-constructed DirectedEdge structs in Go code missing Entity; RDF N-Quads with blank nodes that resolved to 0; client code building mutations programmatically and leaving the subject unset.","solutions":["Set edge.Entity to the real UID obtained from the query or from assigning UIDs via the zero/blank-node flow","If using RDF with blank nodes ( _:alice ), let Dgraph assign UIDs instead of sending 0","Add a caller-side check: reject edges with Entity == 0 before submitting the mutation","For deletes with '*' as subject, use the wildcard-delete API rather than constructing an Entity=0 edge"],"exampleFix":"// before\nedge := &pb.DirectedEdge{Attr: \"name\", Value: []byte(\"Alice\"), ValueType: pb.Posting_STRING}\n// Entity is 0 -> invalid UID with value 0\n// after\nedge := &pb.DirectedEdge{Entity: 0x1001, Attr: \"name\", Value: []byte(\"Alice\"), ValueType: pb.Posting_STRING}","handlingStrategy":"validation","validationCode":"func validEdge(e *pb.DirectedEdge) bool {\n    return e != nil && e.Entity != 0 && e.Attr != \"\"\n}\nif !validEdge(edge) {\n    return fmt.Errorf(\"refusing to submit mutation: UID must be non-zero\")\n}","typeGuard":"func hasUID(e *pb.DirectedEdge) bool { return e != nil && e.Entity > 0 }","tryCatchPattern":"err := AddMutationWithIndex(ctx, edge, startTs)\nif err != nil && strings.Contains(err.Error(), \"invalid UID with value 0\") {\n    return fmt.Errorf(\"edge for attr %q has Entity=0; fetch or assign a UID first\", edge.Attr)\n}","preventionTips":["Always obtain UIDs from a query or the UID assignment flow before building edges","Never hand-set Entity to 0; use blank nodes (_:name) in RDF so Dgraph assigns UIDs","Add unit-test assertions that all constructed edges carry a positive Entity","For wildcard deletes, use the dedicated delete-all/wildcard APIs instead of Entity=0 edges"],"tags":["uid","mutation","indexing","validation"],"backgroundTag":"invalid-uid-zero","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}