{"record":{"id":"c1d7d42865d90e4c","repo":"dgraph-io/dgraph","slug":"subject-should-be-0-for-nquad-v","errorCode":null,"errorMessage":"Subject should be > 0 for nquad: %+v","messagePattern":"Subject should be > 0 for nquad: %\\+v","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/mutation.go","lineNumber":178,"sourceCode":"\t}\n\n\tif err := copyValue(out, nq); err != nil {\n\t\treturn &emptyEdge, err\n\t}\n\treturn out, nil\n}\n\n// ToEdgeUsing determines the UIDs for the provided XIDs and populates the\n// xidToUid map.\nfunc (nq NQuad) ToEdgeUsing(newToUid map[string]uint64) (*pb.DirectedEdge, error) {\n\tvar edge *pb.DirectedEdge\n\tsUid, err := toUid(nq.Subject, newToUid)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif sUid == 0 {\n\t\treturn nil, errors.Errorf(\"Subject should be > 0 for nquad: %+v\", nq)\n\t}\n\n\tswitch nq.valueType() {\n\tcase x.ValueUid:\n\t\toUid, err := toUid(nq.ObjectId, newToUid)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif oUid == 0 {\n\t\t\treturn nil, errors.Errorf(\"ObjectId should be > 0 for nquad: %+v\", nq)\n\t\t}\n\t\tedge = nq.CreateUidEdge(sUid, oUid)\n\tcase x.ValuePlain, x.ValueMulti:\n\t\tedge, err = nq.CreateValueEdge(sUid)\n\tdefault:\n\t\treturn &emptyEdge, errors.Errorf(\"Unknown value type for nquad: %+v\", nq)\n\t}\n\tif err != nil {","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/mutation.go#L160-L196","documentation":"ToEdgeUsing converts a parsed N-Quad into a Dgraph edge, resolving the subject node to a UID via toUid (using newToUid for blank-node mapping). If the resolved subject UID is 0, there is no valid node to anchor the edge, so the library refuses to build it. UID 0 is reserved/invalid in Dgraph, and an edge with a zero subject would be silently corrupt, so this is an explicit fail-fast check.","triggerScenarios":"Calling ToEdgeUsing (directly or via mutation building from dql.ParseMutation/Parse) with an N-Quad whose subject is a blank node like _:alice that is not present in (and cannot be assigned from) the newToUid map, or whose subject resolves to UID 0.","commonSituations":"Building mutations from raw RDF strings where a blank-node label is misspelled or inconsistently reused across quads; reusing a toUid map where the entry was never populated; hand-constructed api.NQuad objects with empty/zero subject UIDs.","solutions":["Check the N-Quad's subject string for typos and ensure blank-node labels (_:name) are consistent across all quads in the mutation","Ensure the newToUid map passed to ToEdgeUsing assigns a non-zero UID for every blank node referenced as a subject","If using explicit UIDs, verify the subject UID is a real, previously-assigned UID (never 0)","Log/print the full nquad (%+v) included in the error to see exactly which subject failed resolution"],"exampleFix":"// before\nmu, _ := dql.MakeMutationEdge(nq, nil) // _:alice missing from map\n// after\ntoUid := map[string]uint64{\"_:alice\": 0x1001}\nedge, err := ToEdgeUsing(nq, toUid)\nif err != nil { return fmt.Errorf(\"quad %v: %w\", nq, err) }","handlingStrategy":"validation","validationCode":"func validSubject(nq *api.NQuad, toUid map[string]uint64) bool {\n  if nq.Subject == \"\" { return false }\n  if uid, err := strconv.ParseUint(nq.Subject, 0, 64); err == nil { return uid != 0 }\n  if strings.HasPrefix(nq.Subject, \"_:\") { return toUid[nq.Subject] != 0 }\n  return false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep one canonical map of blank-node labels to UIDs for the whole mutation","Never hardcode UID 0; it is reserved in Dgraph","Validate N-Quads before building mutations, not after","Log the full nquad from the error to pinpoint the failing subject"],"tags":["dgraph","dql","mutation","uid-resolution"],"backgroundTag":"invalid-uid-in-mutation","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}