{"record":{"id":"962c8f2c42f414e0","repo":"dgraph-io/dgraph","slug":"from-variable-s-should-only-expand-to-1-uid","errorCode":null,"errorMessage":"from variable(%s) should only expand to 1 uid","messagePattern":"from variable\\((.+?)\\) should only expand to 1 uid","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/query.go","lineNumber":1781,"sourceCode":"}\n\n// fillShortestPathVars reads value of the uid variable from mp map and fills it into From and To\n// parameters.\nfunc (sg *SubGraph) fillShortestPathVars(mp map[string]varValue) error {\n\t// The uidVar.Uids can be nil or have an empty uid list if the variable didn't\n\t// return any uids. This would mean sg.Params.From or sg.Params.To is 0 and the\n\t// query would return an empty result.\n\n\tif sg.Params.ShortestPathArgs.From != nil && len(sg.Params.ShortestPathArgs.From.NeedsVar) > 0 {\n\t\tfromVar := sg.Params.ShortestPathArgs.From.NeedsVar[0].Name\n\t\tuidVar, ok := mp[fromVar]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"value of from var(%s) should have already been populated\",\n\t\t\t\tfromVar)\n\t\t}\n\t\tif uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {\n\t\t\tif len(uidVar.Uids.Uids) > 1 {\n\t\t\t\treturn errors.Errorf(\"from variable(%s) should only expand to 1 uid\", fromVar)\n\t\t\t}\n\t\t\tsg.Params.From = uidVar.Uids.Uids[0]\n\t\t}\n\t}\n\n\tif sg.Params.ShortestPathArgs.To != nil && len(sg.Params.ShortestPathArgs.To.NeedsVar) > 0 {\n\t\ttoVar := sg.Params.ShortestPathArgs.To.NeedsVar[0].Name\n\t\tuidVar, ok := mp[toVar]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"value of to var(%s) should have already been populated\",\n\t\t\t\ttoVar)\n\t\t}\n\t\tif uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {\n\t\t\tif len(uidVar.Uids.Uids) > 1 {\n\t\t\t\treturn errors.Errorf(\"to variable(%s) should only expand to 1 uid\", toVar)\n\t\t\t}\n\t\t\tsg.Params.To = uidVar.Uids.Uids[0]\n\t\t}","sourceCodeStart":1763,"sourceCodeEnd":1799,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/query.go#L1763-L1799","documentation":"Shortest-path queries require exactly one start node. When the 'from' endpoint comes from a variable, Dgraph checks that the variable resolves to at most one UID; if it expands to multiple UIDs the path is ambiguous and this error is thrown.","triggerScenarios":"shortest(from: var(f), ...) where var(f) matched multiple nodes, e.g. 'f as var(func: eq(type, \"Person\"))' matching many people, or a uid variable accumulated multiple UIDs.","commonSituations":"Filter too broad in the variable-emitting clause; using a variable populated from a multi-result query; expecting Dgraph to pick a node automatically.","solutions":["Narrow the variable's query so it matches exactly one node (add a unique filter like eq(email, ...) or first: 1)","Use a literal UID in shortest(from:) if the node is known","Use a more specific predicate to disambiguate","Inspect the variable's contents by running the emitting clause alone"],"exampleFix":"// before: matches many\nf as var(func: eq(type, \"Person\"))\np(func: uid(0x1)) { shortest(from: var(f), to: 0x2) { uid } }\n// after: exactly one\nf as var(func: eq(email, \"alice@example.com\"))\np(func: uid(0x1)) { shortest(from: var(f), to: 0x2) { uid } }","handlingStrategy":"validation","validationCode":"// Run the emitting clause first and assert it yields exactly one node\nif countNodes(emittingClause) != 1 {\n    return fmt.Errorf(\"from-variable must resolve to exactly one node\")\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"should only expand to 1 uid\") && strings.Contains(err.Error(), \"from\") {\n    return fmt.Errorf(\"from-variable ambiguous; narrow the filter: %w\", err)\n}","preventionTips":["Filter variable clauses on a unique predicate (email, external id)","Add first: 1 when a single node must be selected","Assert variable cardinality in integration tests"],"tags":["dgraph","query","shortest-path","variables"],"backgroundTag":"shortest-path-ambiguous-variable","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}