dgraph-io/dgraph · error

%s not allowed multiple times in same sub-query.

Error message

%s not allowed multiple times in same sub-query.

What it means

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.

Source

Thrown at query/query.go:548

	// Typically you act on the current node, and leave recursion to deal with
	// children. But, in this case, we don't want to muck with the current
	// node, because of the way we're dealing with the root node.
	// So, we work on the children, and then recurse for grand children.
	attrsSeen := make(map[string]struct{})

	for _, gchild := range gq.Children {
		if sg.Params.Alias == "shortest" && gchild.Expand != "" {
			return errors.Errorf("expand() not allowed inside shortest")
		}

		key := ""
		if gchild.Alias != "" {
			key = gchild.Alias
		} else {
			key = uniqueKey(gchild)
		}
		if _, ok := attrsSeen[key]; ok {
			return errors.Errorf("%s not allowed multiple times in same sub-query.",
				key)
		}
		attrsSeen[key] = struct{}{}

		args := params{
			Alias:        gchild.Alias,
			Expand:       gchild.Expand,
			Facet:        gchild.Facets,
			FacetsOrder:  gchild.FacetsOrder,
			FacetVar:     gchild.FacetVar,
			GetUid:       sg.Params.GetUid,
			IgnoreReflex: sg.Params.IgnoreReflex,
			Langs:        gchild.Langs,
			NeedsVar:     append(gchild.NeedsVar[:0:0], gchild.NeedsVar...),
			Normalize:    gchild.Normalize || sg.Params.Normalize,
			Order:        gchild.Order,
			Var:          gchild.Var,
			GroupbyAttrs: gchild.GroupbyAttrs,

View on GitHub (pinned to 759e242be6)

Solutions

  1. Give each duplicate child a distinct alias, e.g. `friend1 as friend` and `friend2 as friend`
  2. Remove the redundant duplicate line if it is unintentional
  3. If grouping is needed, nest the duplicates under different parent levels or use groupby

Example fix

// before
{
  director(film: [0x1]) {
    friend
    friend
  }
}
// after
{
  director(film: [0x1]) {
    f1 as friend
    f2 as friend
  }
}
Defensive patterns

Strategy: validation

Validate before calling

function checkDuplicateKeys(level) {
  const seen = new Set();
  for (const child of level.children) {
    const key = child.alias || child.predicate;
    if (seen.has(key)) throw new Error(`Duplicate key "${key}" in same sub-query level`);
    seen.add(key);
  }
}

Type guard

function hasUniqueKeys(children) { const keys = children.map(c => c.alias || c.predicate); return new Set(keys).size === keys.length; }

Try / catch

try {
  return await txn.query(q);
} catch (e) {
  if (e.message.includes('not allowed multiple times in same sub-query')) {
    console.error('Duplicate alias/predicate at one level:', e.message);
  }
  throw e;
}

Prevention

When it happens

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

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

Related errors


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