{"record":{"id":"d35b0651ee2bb743","repo":"dgraph-io/dgraph","slug":"error-while-building-unique-query","errorCode":null,"errorMessage":"error while building unique query","messagePattern":"error while building unique query","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"edgraph/server.go","lineNumber":1997,"sourceCode":"\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tval := strconv.Quote(fmt.Sprintf(\"%v\", dql.TypeValFrom(pred.ObjectValue).Value))\n\t\t\t\tquery := fmt.Sprintf(`%v as var(func: eq(%v,\"%v\"))`, queryVar, predicateName, val[1:len(val)-1])\n\t\t\t\tif _, err := buildQuery.WriteString(query); err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"error while writing string\")\n\t\t\t\t}\n\t\t\t\tqc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{queryVar: queryVar}\n\t\t\t} else {\n\t\t\t\tif err := validateValObjectId(pred.ObjectId); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tvalQueryVar := fmt.Sprintf(\"__dgraph_uniquecheck_val_%v__\", uniqueVarMapKey)\n\t\t\t\tquery := fmt.Sprintf(`%v as var(func: eq(%v,%v)){\n\t\t\t\t\t                             uid\n\t\t\t\t\t                             %v as %v\n\t\t\t\t                 }`, queryVar, predicateName, pred.ObjectId, valQueryVar, predicateName)\n\t\t\t\tif _, err := buildQuery.WriteString(query); err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"error while building unique query\")\n\t\t\t\t}\n\t\t\t\tqc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{valVar: valQueryVar, queryVar: queryVar}\n\t\t\t}\n\t\t}\n\n\t\tif buildQuery.Len() > 0 {\n\t\t\tif _, err := buildQuery.WriteString(\"}\"); err != nil {\n\t\t\t\treturn errors.Wrapf(err, \"error while writing string\")\n\t\t\t}\n\t\t\tif qc.req.Query == \"\" {\n\t\t\t\tqc.req.Query = \"{\" + buildQuery.String()\n\t\t\t} else {\n\t\t\t\tqc.req.Query = strings.TrimSuffix(qc.req.Query, \"}\")\n\t\t\t\tqc.req.Query = qc.req.Query + buildQuery.String()\n\t\t\t}\n\t\t}\n\t}\n\treturn nil","sourceCodeStart":1979,"sourceCodeEnd":2015,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/edgraph/server.go#L1979-L2015","documentation":"For N-Quads whose object is a val(var) variable on a @unique predicate, addQueryIfUnique builds a multi-line DQL query block `X as var(func: eq(P, val(v))){ uid Y as P }`. A WriteString failure while appending this block is wrapped as 'error while building unique query' (edgraph/server.go:1997). Like error 275, it is an internal builder failure triggered from the unique-check query construction path, not a client-side mistake.","triggerScenarios":"Internal strings.Builder write failure while constructing the unique-check query for a mutation N-Quad with object val(varName) on a @unique predicate (edgraph/server.go:1997). Note: an invalid val() reference inside pred.ObjectId is rejected earlier by validateValObjectId with a different error, so this specific message indicates a builder-level failure.","commonSituations":"Alpha under memory pressure or in a degraded state; essentially never attributable to mutation content beyond it triggering the val()-based unique-check path.","solutions":["Retry the mutation; the failure is typically transient (allocation pressure)","Check Alpha memory usage/logs for the underlying cause and raise container memory limits if under pressure","Restart the Alpha if errors persist","File an issue with Dgraph including the wrapped error if it reproduces consistently"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := mutate(ctx, mu)\nif err != nil && strings.Contains(err.Error(), \"error while building unique query\") {\n\t// internal/transient: retry with backoff; escalate if persistent\n}","preventionTips":["Ensure Alphas have adequate memory headroom","Keep Dgraph upgraded to a version where this internal failure is not observed","Log full wrapped causes for escalation","Restart degraded Alphas rather than retrying indefinitely"],"tags":["dgraph","internal","mutation","unique-constraint"],"backgroundTag":"internal-write-failure","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}