{"record":{"id":"5db4eb1a574c9f05","repo":"dgraph-io/dgraph","slug":"only-uid-predicate-is-allowed-in-count-within-grou","errorCode":null,"errorMessage":"Only uid predicate is allowed in count within groupby","messagePattern":"Only uid predicate is allowed in count within groupby","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/groupby.go","lineNumber":35,"sourceCode":"\t\"github.com/dgraph-io/dgraph/v25/types\"\n)\n\ntype groupPair struct {\n\tkey  types.Val\n\tattr string\n}\n\ntype groupResult struct {\n\tkeys       []groupPair\n\taggregates []groupPair\n\tuids       []uint64\n}\n\nfunc (grp *groupResult) aggregateChild(child *SubGraph) error {\n\tfieldName := child.Params.Alias\n\tif child.Params.DoCount {\n\t\tif child.Attr != \"uid\" {\n\t\t\treturn errors.Errorf(\"Only uid predicate is allowed in count within groupby\")\n\t\t}\n\t\tif fieldName == \"\" {\n\t\t\tfieldName = \"count\"\n\t\t}\n\t\tgrp.aggregates = append(grp.aggregates, groupPair{\n\t\t\tattr: fieldName,\n\t\t\tkey: types.Val{\n\t\t\t\tTid:   types.IntID,\n\t\t\t\tValue: int64(len(grp.uids)),\n\t\t\t},\n\t\t})\n\t\treturn nil\n\t}\n\tif child.SrcFunc != nil && isAggregatorFn(child.SrcFunc.Name) {\n\t\tif fieldName == \"\" {\n\t\t\tfieldName = fmt.Sprintf(\"%s(%s)\", child.SrcFunc.Name, child.Attr)\n\t\t}\n\t\tfinalVal, err := aggregateGroup(grp, child)","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/groupby.go#L17-L53","documentation":"In groupby queries, a child SubGraph with DoCount enabled (count(predicate)) must target the special \"uid\" predicate, because counting within groups is only implemented for uid edges. aggregateChild rejects any other attribute with this error before building the group aggregates.","triggerScenarios":"A groupby query containing count(somePredicate) where somePredicate is a regular scalar or index predicate rather than count(uid).","commonSituations":"Developers try to count values of a normal predicate per group; copying a normal query's count() syntax into groupby where only count(uid) is supported; version-specific groupby limitations.","solutions":["Use count(uid) inside groupby to count nodes per group","Move the counting of a non-uid predicate outside groupby (e.g. fetch values and count client-side, or run a separate aggregation query)","Restructure the query: groupby on the key and use a nested/second query for per-group counts of the predicate","Check Dgraph docs/version for supported groupby aggregation syntax"],"exampleFix":"// before\ngroupby(q(func: type(Person)) { count(friend) @groupby(city) })\n// after\n{\n  g(func: type(Person)) @groupby(city) { count(uid) }\n}","handlingStrategy":"validation","validationCode":"function validateGroupbyCount(child) {\n  if (child.count && child.predicate !== 'uid') {\n    throw new Error('groupby count supports only count(uid), got: ' + child.predicate);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  r = await txn.query(q);\n} catch (e) {\n  if (String(e).includes('Only uid predicate is allowed in count within groupby')) {\n    // rewrite query to use count(uid) or aggregate client-side\n  }\n  throw e;\n}","preventionTips":["Use count(uid) exclusively inside groupby blocks","Compute per-group counts of other predicates in application code or separate queries","Review groupby docs for supported aggregations in your Dgraph version","Add query linter rules rejecting count(non-uid) in groupby"],"tags":["dgraph","query","groupby","count"],"backgroundTag":"invalid-groupby-aggregation","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}