{"record":{"id":"70ae9d4ed6e7c32f","repo":"dgraph-io/dgraph","slug":"expected-one-uid-for-var-in-groupby-but-got-d","errorCode":null,"errorMessage":"Expected one UID for var in groupby but got: %d","messagePattern":"Expected one UID for var in groupby but got: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/groupby.go","lineNumber":343,"sourceCode":"\t\t// This is a aggregation node.\n\t\tfor _, grp := range res.group {\n\t\t\terr := grp.aggregateChild(child)\n\t\t\tif err != nil && err != ErrEmptyVal {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\tif child.Params.Var == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tchVar := child.Params.Var\n\n\t\ttempMap := types.NewShardedMap()\n\t\tfor _, grp := range res.group {\n\t\t\tif len(grp.keys) == 0 {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif len(grp.keys) > 1 {\n\t\t\t\treturn errors.Errorf(\"Expected one UID for var in groupby but got: %d\", len(grp.keys))\n\t\t\t}\n\t\t\tuidVal := grp.keys[0].key.Value\n\t\t\tuid, ok := uidVal.(uint64)\n\t\t\tif !ok {\n\t\t\t\treturn errors.Errorf(\"Vars can be assigned only when grouped by UID attribute\")\n\t\t\t}\n\t\t\t// grp.aggregates could be empty if schema conversion failed during aggregation\n\t\t\tif len(grp.aggregates) > 0 {\n\t\t\t\ttempMap.Set(uid, grp.aggregates[len(grp.aggregates)-1].key)\n\t\t\t}\n\t\t}\n\t\tdoneVars[chVar] = varValue{\n\t\t\tVals: tempMap,\n\t\t\tpath: append(path, pathNode),\n\t\t}\n\t}\n\treturn nil\n}","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/groupby.go#L325-L361","documentation":"fillGroupedVars assigns a GraphQL+- math/groupby variable from a groupby result. It expects each group to have exactly one key (the UID attribute grouped on); more than one key means the variable cannot be mapped back to a single UID, so Dgraph rejects the query.","triggerScenarios":"A query uses AS var inside a groupby where the groupby block groups on more than one attribute (or the aggregation produces multiple keys per group), so grp.keys has length > 1 when fillGroupedVars runs via processGroupBy.","commonSituations":"Writing `groupby(uid, name) { total as sum(amount) }` and then using `total` elsewhere; grouping by multiple predicates while also exporting a variable; refactoring a single-attribute groupby into multi-attribute without removing the AS var.","solutions":["Group by exactly one UID-valued attribute (typically uid) when exporting a variable with AS","Remove the AS variable export from the multi-attribute groupby and compute it in a separate query or subquery","Move the extra grouping attribute out of groupby or into a parent query block"],"exampleFix":"// before\nquery {\n  me(func: type(User)) {\n    groupby(uid, name) {\n      total as count(friends)\n    }\n    sum(total)\n  }\n}\n// after\nquery {\n  me(func: type(User)) {\n    groupby(uid) {\n      total as count(friends)\n    }\n    sum(total)\n  }\n}","handlingStrategy":"validation","validationCode":"// Before exporting a var from groupby, ensure exactly one grouping attribute:\n// BAD:  groupby(uid, name) { total as count(friends) }\n// GOOD: groupby(uid) { total as count(friends) }\nfunction validateGroupbyVarExport(groupbyAttrs, exportsVar) {\n  if (exportsVar && groupbyAttrs.length !== 1) {\n    throw new Error('AS var in groupby requires exactly one (UID) grouping attribute');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const res = await dgraph.newTxn().query(query);\n} catch (e) {\n  if (String(e).includes('Expected one UID for var in groupby')) {\n    // rewrite query: reduce groupby to a single UID attribute\n  } else throw e;\n}","preventionTips":["Always pair AS variable exports in groupby with a single uid grouping attribute","Keep multi-attribute groupbys free of AS exports","Lint query templates for `groupby(` with more than one argument followed by ` as `"],"tags":["dgraph","graphql-plus","groupby","query-validation"],"backgroundTag":"groupby-variable-misuse","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}