{"record":{"id":"2ee380aff4248f32","repo":"dgraph-io/dgraph","slug":"node-with-q-cant-have-filter-please-place-the-fi","errorCode":null,"errorMessage":"Node with %q cant have filter, please place the filter on the upper level","messagePattern":"Node with %q cant have filter, please place the filter on the upper level","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/query.go","lineNumber":629,"sourceCode":"\t\tif gchild.MathExp != nil {\n\t\t\tmathExp := &mathTree{}\n\t\t\tif err := mathCopy(mathExp, gchild.MathExp); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdst.MathExp = mathExp\n\t\t}\n\n\t\tif gchild.Func != nil &&\n\t\t\t(gchild.Func.IsAggregator() || gchild.Func.IsPasswordVerifier()) {\n\t\t\tif len(gchild.Children) != 0 {\n\t\t\t\treturn errors.Errorf(\"Node with %q cant have child attr\", gchild.Func.Name)\n\t\t\t}\n\t\t\t// embedded filter will cause ambiguous output like following,\n\t\t\t// director.film @filter(gt(initial_release_date, \"2016\")) {\n\t\t\t//    min(initial_release_date @filter(gt(initial_release_date, \"1986\"))\n\t\t\t// }\n\t\t\tif gchild.Filter != nil {\n\t\t\t\treturn errors.Errorf(\n\t\t\t\t\t\"Node with %q cant have filter, please place the filter on the upper level\",\n\t\t\t\t\tgchild.Func.Name)\n\t\t\t}\n\t\t\tif gchild.Func.Attr == \"uid\" {\n\t\t\t\treturn errors.Errorf(`Argument cannot be \"uid\"`)\n\t\t\t}\n\t\t\tdst.createSrcFunction(gchild.Func)\n\t\t}\n\n\t\tif gchild.Filter != nil {\n\t\t\tdstf := &SubGraph{}\n\t\t\tif err := filterCopy(dstf, gchild.Filter); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdst.Filters = append(dst.Filters, dstf)\n\t\t}\n\n\t\tif gchild.FacetsFilter != nil {","sourceCodeStart":611,"sourceCodeEnd":647,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/query.go#L611-L647","documentation":"A node carrying an aggregator or password-verifier function must not also carry an @filter directive. An embedded filter under an aggregate is ambiguous — e.g. `min(...) @filter(...)` inside another filter — so treeCopy rejects it and asks that the filter be placed at the parent/upper level instead.","triggerScenarios":"Query like `director @filter(...)` where the child is `min(initial_release_date) @filter(gt(initial_release_date, \"1986\"))` — i.e. a filter attached to the aggregated node itself.","commonSituations":"Attempting to filter values before aggregation in a single expression; copy-paste of filters across levels; older queries retargeted after adding an aggregate.","solutions":["Move the @filter to the parent level so filtering happens on the set before aggregation","Filter the scalar predicate inside the parent's filter: e.g. `friend @filter(gt(score, 3)) { min(score) }`","If filtering aggregated results is needed, store the aggregate in a variable and apply it in a func/filter at the root"],"exampleFix":"// before\n{\n  me(func: uid(0x1)) {\n    friend {\n      min(initial_release_date) @filter(gt(initial_release_date, \"1986\"))\n    }\n  }\n}\n// after\n{\n  me(func: uid(0x1)) {\n    friend @filter(gt(initial_release_date, \"1986\")) {\n      min(initial_release_date)\n    }\n  }\n}","handlingStrategy":"validation","validationCode":"function assertNoFilterOnAggregate(node) {\n  if (/^(min|max|sum|avg|count)\\s*\\(/.test(node.field) && node.filter) {\n    throw new Error('Move the @filter from the aggregated node to the parent level');\n  }\n}","typeGuard":"const isAggregateWithFilter = (n) => Boolean(n.func && n.func.isAggregator && n.func.isAggregator() && n.filter);","tryCatchPattern":"try {\n  await txn.query(q);\n} catch (e) {\n  if (e.message.includes('cant have filter')) {\n    throw new Error('Relocate @filter to the parent level of the aggregate');\n  }\n  throw e;\n}","preventionTips":["Attach @filter to the parent edge, never to an aggregate node","To filter before aggregation, filter the scalar predicate at the parent","To filter after aggregation, use aggregator variables in a root func/filter"],"tags":["dgraph","query","filter","aggregation"],"backgroundTag":"invalid-filter-placement","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}