dgraph-io/dgraph · error
Node with count cannot have child attributes
Error message
Node with count cannot have child attributes
What it means
A count(...) aggregation node reduces a level to a single number and therefore cannot itself contain child attributes. treeCopy checks gchild.IsCount and rejects any node with children, since there is no place to attach their results. The count must be a leaf of the query tree.
Source
Thrown at query/query.go:589
// Inherit from the parent.
if len(sg.Params.Cascade.Fields) > 0 {
args.Cascade.Fields = append(args.Cascade.Fields, sg.Params.Cascade.Fields...)
}
// Allow over-riding at this level.
if len(gchild.Cascade) > 0 {
args.Cascade.Fields = gchild.Cascade
}
// Remove pagination arguments from the query if @cascade is mentioned since
// pagination will be applied post processing the data.
if len(args.Cascade.Fields) > 0 {
args.addCascadePaginationArguments(gchild)
}
if gchild.IsCount {
if len(gchild.Children) != 0 {
return errors.New("Node with count cannot have child attributes")
}
args.DoCount = true
}
for argk := range gchild.Args {
if !isValidArg(argk) {
return errors.Errorf("Invalid argument: %s", argk)
}
}
if err := args.fill(gchild); err != nil {
return err
}
if len(args.Order) != 0 && len(args.FacetsOrder) != 0 {
return errors.Errorf("Cannot specify order at both args and facets")
}
dst := &SubGraph{View on GitHub (pinned to 759e242be6)
Solutions
- Remove the child attributes and make the count a leaf: `count(friend)`
- Fetch the attributes in a sibling level: one branch counts, a sibling branch lists children
- Use two queries or aliases if both the count and the sample of children are required
Example fix
// before
{
me(func: uid(0x1)) {
count(friend) {
name
}
}
}
// after
{
me(func: uid(0x1)) {
total as count(friend)
friend {
name
}
}
} Defensive patterns
Strategy: validation
Validate before calling
function assertCountIsLeaf(node) {
if (/^count\s*\(/.test(node.field) && Array.isArray(node.children) && node.children.length > 0) {
throw new Error('count(...) node must not have child attributes');
}
} Type guard
const isCountWithChildren = (n) => n.isCount === true && n.children && n.children.length > 0;
Try / catch
try {
await txn.query(q);
} catch (e) {
if (e.message === 'Node with count cannot have child attributes') {
throw new Error('Move child attributes to a sibling level of the count');
}
throw e;
} Prevention
- Treat count(...) nodes as leaves always
- Fetch attributes in a sibling branch with an alias
- Query builders: skip child emission when the field is wrapped in count()
When it happens
Trigger: Writing `count(friend) { name }` or `friend { count(uid) { name } }` — any count node followed by a brace block with children.
Common situations: Users trying to count and fetch attributes in one traversal; migrating SQL-style SELECT COUNT(col), other_col mental models into Dgraph; query builders that attach children to every node.
Related errors
- Only uid predicate is allowed in count within groupby
- expand() not allowed inside shortest
- Invalid argument: %s
- Node with %q cant have child attr
- count(predicate) cannot be used to search for negative count
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/33e97db0e7b712c2.
Report an issue: GitHub.