{"record":{"id":"ea1a2bfb38496ba9","repo":"dgraph-io/dgraph","slug":"s-not-allowed-multiple-times-in-same-sub-query","errorCode":null,"errorMessage":"%s not allowed multiple times in same sub-query.","messagePattern":"(.+?) not allowed multiple times in same sub-query\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/query.go","lineNumber":548,"sourceCode":"\t// Typically you act on the current node, and leave recursion to deal with\n\t// children. But, in this case, we don't want to muck with the current\n\t// node, because of the way we're dealing with the root node.\n\t// So, we work on the children, and then recurse for grand children.\n\tattrsSeen := make(map[string]struct{})\n\n\tfor _, gchild := range gq.Children {\n\t\tif sg.Params.Alias == \"shortest\" && gchild.Expand != \"\" {\n\t\t\treturn errors.Errorf(\"expand() not allowed inside shortest\")\n\t\t}\n\n\t\tkey := \"\"\n\t\tif gchild.Alias != \"\" {\n\t\t\tkey = gchild.Alias\n\t\t} else {\n\t\t\tkey = uniqueKey(gchild)\n\t\t}\n\t\tif _, ok := attrsSeen[key]; ok {\n\t\t\treturn errors.Errorf(\"%s not allowed multiple times in same sub-query.\",\n\t\t\t\tkey)\n\t\t}\n\t\tattrsSeen[key] = struct{}{}\n\n\t\targs := params{\n\t\t\tAlias:        gchild.Alias,\n\t\t\tExpand:       gchild.Expand,\n\t\t\tFacet:        gchild.Facets,\n\t\t\tFacetsOrder:  gchild.FacetsOrder,\n\t\t\tFacetVar:     gchild.FacetVar,\n\t\t\tGetUid:       sg.Params.GetUid,\n\t\t\tIgnoreReflex: sg.Params.IgnoreReflex,\n\t\t\tLangs:        gchild.Langs,\n\t\t\tNeedsVar:     append(gchild.NeedsVar[:0:0], gchild.NeedsVar...),\n\t\t\tNormalize:    gchild.Normalize || sg.Params.Normalize,\n\t\t\tOrder:        gchild.Order,\n\t\t\tVar:          gchild.Var,\n\t\t\tGroupbyAttrs: gchild.GroupbyAttrs,","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/query.go#L530-L566","documentation":"Within a single sub-query level, each key (child alias or generated unique key from the predicate) must be unique because Dgraph serializes results into JSON keyed by these names. treeCopy tracks keys in attrsSeen and rejects a second occurrence at the same level. Repeated keys would silently overwrite each other in the response, so it is a parse-time error.","triggerScenarios":"A query level contains two children with the same alias (`a as x` twice, or two `friend` lines at the same level without distinct aliases), or two predicates that normalize to the same uniqueKey with no alias to disambiguate.","commonSituations":"Copy-pasted blocks in large queries; programmatically generated queries appending the same facet/count twice; users expecting Dgraph to merge duplicate lines like some GraphQL engines merge fields.","solutions":["Give each duplicate child a distinct alias, e.g. `friend1 as friend` and `friend2 as friend`","Remove the redundant duplicate line if it is unintentional","If grouping is needed, nest the duplicates under different parent levels or use groupby"],"exampleFix":"// before\n{\n  director(film: [0x1]) {\n    friend\n    friend\n  }\n}\n// after\n{\n  director(film: [0x1]) {\n    f1 as friend\n    f2 as friend\n  }\n}","handlingStrategy":"validation","validationCode":"function checkDuplicateKeys(level) {\n  const seen = new Set();\n  for (const child of level.children) {\n    const key = child.alias || child.predicate;\n    if (seen.has(key)) throw new Error(`Duplicate key \"${key}\" in same sub-query level`);\n    seen.add(key);\n  }\n}","typeGuard":"function hasUniqueKeys(children) { const keys = children.map(c => c.alias || c.predicate); return new Set(keys).size === keys.length; }","tryCatchPattern":"try {\n  return await txn.query(q);\n} catch (e) {\n  if (e.message.includes('not allowed multiple times in same sub-query')) {\n    console.error('Duplicate alias/predicate at one level:', e.message);\n  }\n  throw e;\n}","preventionTips":["Alias every repeated predicate in the same level","Programmatic query builders should auto-suffix duplicate aliases (f1, f2, ...)","Validate query JSON keys for uniqueness before serialization"],"tags":["dgraph","query","alias","duplicate-key"],"backgroundTag":"duplicate-field-key","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}