{"record":{"id":"ff32be50fb723ab9","repo":"cayleygraph/cayley","slug":"cannot-unnest-more-than-one-object-on-q-use-s","errorCode":null,"errorMessage":"cannot unnest more than one object on %q; use (%s: 1) to force","messagePattern":"cannot unnest more than one object on %q; use \\((.+?): 1\\) to force","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/graphql/graphql.go","lineNumber":375,"sourceCode":"\t\t\tp2 := path.StartPathNodes(qs, r.id)\n\t\t\tif len(f2.Labels) != 0 {\n\t\t\t\tp2 = p2.LabelContext(f2.Labels)\n\t\t\t}\n\t\t\tif f2.Rev {\n\t\t\t\tp2 = p2.In(f2.Via)\n\t\t\t} else {\n\t\t\t\tp2 = p2.Out(f2.Via)\n\t\t\t}\n\t\t\tif len(f2.Labels) != 0 {\n\t\t\t\tp2 = p2.LabelContext()\n\t\t\t}\n\t\t\tarr, err := iterateObject(ctx, qs, &f2, p2)\n\t\t\tif err != nil {\n\t\t\t\treturn out, err\n\t\t\t}\n\t\t\tif f2.UnNest {\n\t\t\t\tif len(arr) > 1 {\n\t\t\t\t\treturn nil, fmt.Errorf(\"cannot unnest more than one object on %q; use (%s: 1) to force\",\n\t\t\t\t\t\tf2.Alias, LimitKey)\n\t\t\t\t} else if len(arr) == 0 {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tfor k, v := range arr[0] {\n\t\t\t\t\tobj[k] = v\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tvar v interface{}\n\t\t\t\tif len(arr) == 1 {\n\t\t\t\t\tv = arr[0]\n\t\t\t\t} else if len(arr) > 1 {\n\t\t\t\t\tv = arr\n\t\t\t\t}\n\t\t\t\tobj[f2.Alias] = v\n\t\t\t}\n\t\t}\n\t\tout = append(out, obj)","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/query/graphql/graphql.go#L357-L393","documentation":"When a nested object field is marked unnested (f2.UnNest), iterateObject inlines its single result row into the parent object; if the nested query produced more than one result, inlining is ambiguous, so the error asks the query author to add an explicit limit of 1 (using the LimitKey) to force unnesting.","triggerScenarios":"A GraphQL query with an unnested nested field whose sub-query matches multiple nodes — e.g. unnesting a multi-valued relation without a limit — hitting query/graphql/graphql.go:375 when len(arr) > 1.","commonSituations":"Unnesting fields like friends/children that naturally return many results, or data changes causing a previously-single result to expand into multiple after new nodes are added.","solutions":["Add the limit directive to the nested field: (id: 1) style — i.e. set LimitKey to 1 — to force unnesting.","Nest the field instead of unnesting so all results are returned as a list.","Tighten the nested query's constraints (has filters) so it matches at most one object."],"exampleFix":"// before\nuser { friends @unnest { name } }\n// after\nuser { friends(limit: 1) @unnest { name } }","handlingStrategy":"fallback","validationCode":"const nested = field.nested; if (nested.unnest && !nested.limit) console.warn('unnested field may match multiple objects; add limit: 1');","typeGuard":null,"tryCatchPattern":"out, err := iterateObject(ctx, qs, &f2, p2)\nif err != nil && strings.Contains(err.Error(), \"cannot unnest more than one object\") { /* add LimitKey=1 or keep nesting */ }","preventionTips":["Always pair unnest with a limit of 1 when cardinality is uncertain","Prefer nested (list) output for multi-valued relations","Re-verify unnested fields after data changes that could add matches"],"tags":["graphql","query","unnest","cardinality"],"backgroundTag":"unexpected-response-shape","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}