dgraph-io/dgraph · error

Expected one UID for var in groupby but got: %d

Error message

Expected one UID for var in groupby but got: %d

What it means

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.

Source

Thrown at query/groupby.go:343

		// This is a aggregation node.
		for _, grp := range res.group {
			err := grp.aggregateChild(child)
			if err != nil && err != ErrEmptyVal {
				return err
			}
		}
		if child.Params.Var == "" {
			continue
		}
		chVar := child.Params.Var

		tempMap := types.NewShardedMap()
		for _, grp := range res.group {
			if len(grp.keys) == 0 {
				continue
			}
			if len(grp.keys) > 1 {
				return errors.Errorf("Expected one UID for var in groupby but got: %d", len(grp.keys))
			}
			uidVal := grp.keys[0].key.Value
			uid, ok := uidVal.(uint64)
			if !ok {
				return errors.Errorf("Vars can be assigned only when grouped by UID attribute")
			}
			// grp.aggregates could be empty if schema conversion failed during aggregation
			if len(grp.aggregates) > 0 {
				tempMap.Set(uid, grp.aggregates[len(grp.aggregates)-1].key)
			}
		}
		doneVars[chVar] = varValue{
			Vals: tempMap,
			path: append(path, pathNode),
		}
	}
	return nil
}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Group by exactly one UID-valued attribute (typically uid) when exporting a variable with AS
  2. Remove the AS variable export from the multi-attribute groupby and compute it in a separate query or subquery
  3. Move the extra grouping attribute out of groupby or into a parent query block

Example fix

// before
query {
  me(func: type(User)) {
    groupby(uid, name) {
      total as count(friends)
    }
    sum(total)
  }
}
// after
query {
  me(func: type(User)) {
    groupby(uid) {
      total as count(friends)
    }
    sum(total)
  }
}
Defensive patterns

Strategy: validation

Validate before calling

// Before exporting a var from groupby, ensure exactly one grouping attribute:
// BAD:  groupby(uid, name) { total as count(friends) }
// GOOD: groupby(uid) { total as count(friends) }
function validateGroupbyVarExport(groupbyAttrs, exportsVar) {
  if (exportsVar && groupbyAttrs.length !== 1) {
    throw new Error('AS var in groupby requires exactly one (UID) grouping attribute');
  }
}

Try / catch

try {
  const res = await dgraph.newTxn().query(query);
} catch (e) {
  if (String(e).includes('Expected one UID for var in groupby')) {
    // rewrite query: reduce groupby to a single UID attribute
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/70ae9d4ed6e7c32f. Report an issue: GitHub.