{"record":{"id":"3246153620a2285e","repo":"dgraph-io/dgraph","slug":"error-while-parsing-v","errorCode":null,"errorMessage":"error while parsing [%v]","messagePattern":"error while parsing \\[(.+?)\\]","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"edgraph/server.go","lineNumber":1806,"sourceCode":"\t\tisEmpty := func(l *pb.List) bool {\n\t\t\treturn l == nil || len(l.Uids) == 0\n\t\t}\n\n\t\tvar subjectUid uint64\n\t\tif strings.HasPrefix(pred.Subject, \"uid(\") {\n\t\t\tvarName := qr.Vars[pred.Subject[4:len(pred.Subject)-1]]\n\t\t\tif isEmpty(varName.Uids) {\n\t\t\t\tsubjectUid = 0 // blank node\n\t\t\t} else if len(varName.Uids.Uids) == 1 {\n\t\t\t\tsubjectUid = varName.Uids.Uids[0]\n\t\t\t} else {\n\t\t\t\treturn errors.Errorf(\"unique constraint violated for predicate [%v]\", pred.Predicate)\n\t\t\t}\n\t\t} else {\n\t\t\tvar err error\n\t\t\tsubjectUid, err = parseSubject(pred.Subject)\n\t\t\tif err != nil {\n\t\t\t\treturn errors.Wrapf(err, \"error while parsing [%v]\", pred.Subject)\n\t\t\t}\n\t\t}\n\n\t\tvar predValue interface{}\n\t\tif strings.HasPrefix(pred.ObjectId, \"val(\") {\n\t\t\tvarName := qr.Vars[pred.ObjectId[4:len(pred.ObjectId)-1]]\n\t\t\tval, ok := varName.Vals.Get(0)\n\t\t\tif !ok {\n\t\t\t\t_, isValueGoingtoSet := varName.Vals.Get(subjectUid)\n\t\t\t\tif !isValueGoingtoSet {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tresults := qr.Vars[queryVar.valVar]\n\t\t\t\terr := results.Vals.Iterate(func(uidOfv uint64, v types.Val) error {\n\t\t\t\t\tvarNameVal, _ := varName.Vals.Get(subjectUid)\n\t\t\t\t\tif v.Value == varNameVal.Value && uidOfv != subjectUid {\n\t\t\t\t\t\treturn errors.Errorf(\"could not insert duplicate value [%v] for predicate [%v]\",","sourceCodeStart":1788,"sourceCodeEnd":1824,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/edgraph/server.go#L1788-L1824","documentation":"This error wraps a failure from parseSubject() while Dgraph processes a mutation against predicates that carry a @unique constraint. During verifyUnique, every N-Quad subject must resolve to a UID: either a uid(var) reference or a literal UID like '0x123'. If the subject string is neither, parseSubject fails and this wrapper adds the offending subject to the message. It indicates a malformed mutation edge, not a uniqueness problem.","triggerScenarios":"Sending a Mutation with an N-Quad whose subject is not a valid UID (0x-hex), not a blank node (_:name), and not a uid(var) reference — e.g. subject set to a plain string, an empty value, or a numeric ID — while the mutation runs through the unique-constraint upsert path (verifyUnique at edgraph/server.go:1806).","commonSituations":"Hand-built RDF strings with typos; JSON mutations where the 'uid' field is sent as a name/email string instead of '0x...' (developers expect upsert-by-value, which only happens via @upsert + query); client SDKs serializing objects whose uid was never assigned; referencing a GraphQL id field instead of the Dgraph UID.","solutions":["Inspect the wrapped message for the offending subject value and fix it in the mutation payload to a valid UID (0x...), blank node (_:name), or uid(var) reference","If you intended to match by value, run an upsert query first (upsert block with 'uid(var)' or 'uid(...)' in the mutation) instead of putting the raw value in the subject position","If the value comes from client input, validate it server-side with a regex like ^0x[0-9a-f]+$ before building the N-Quad","Check SDK serialization: ensure you pass Dgraph.UID objects, not arbitrary strings, when constructing mutations programmatically"],"exampleFix":"// before\n{ \"set\": [ { \"uid\": \"alice@example.com\", \"name\": \"Alice\" } ] }\n// after (upsert by value first)\nupsert(query: \"{ a as var(func: eq(email, \\\"alice@example.com\\\")) }\", mutation: \"{ set { uid(a) <name> \\\"Alice\\\" . } }\")","handlingStrategy":"validation","validationCode":"import \"regexp\"\nvar uidRe = regexp.MustCompile(`^(0x[0-9a-fA-F]+|_:[A-Za-z_][A-Za-z0-9_.-]*|uid\\([^)]+\\))$`)\nfunc validSubject(s string) bool { return uidRe.MatchString(s) }\n// reject or upsert-by-value when validSubject(nquad.Subject) is false","typeGuard":"func isUIDRef(s string) bool {\n\treturn strings.HasPrefix(s, \"0x\") && len(s) >= 3\n}","tryCatchPattern":"err := dgoMutate(ctx, mu)\nif err != nil && strings.Contains(err.Error(), \"error while parsing\") {\n\t// malformed subject: fall back to upsert-by-value flow\n}","preventionTips":["Always use UIDs (0x...) or blank nodes (_:name) as mutation subjects","Use upsert blocks with uid(var) instead of putting values in the subject position","Validate client-supplied UID fields with a 0x-hex regex before building mutations","Pass typed UID objects through dgo instead of raw strings"],"tags":["dgraph","mutation","uid-parsing","upsert"],"backgroundTag":"invalid-mutation-subject-uid","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}