{"record":{"id":"bc629731eb97411c","repo":"hasura/graphql-engine","slug":"unexpected-number-of-rows-rows-length-returne","errorCode":null,"errorMessage":"Unexpected number of rows (${rows.length}) returned by order by element query","messagePattern":"Unexpected number of rows \\((.+?)\\) returned by order by element query","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"dc-agents/reference/src/query.ts","lineNumber":443,"sourceCode":"        },\n      },\n    };\n  }\n};\n\nconst extractResultFromOrderByElementQueryResponse = (\n  orderByElement: OrderByElement,\n  response: QueryResponse,\n): RawScalarValue => {\n  const [relationshipName, ...remainingPath] = orderByElement.target_path;\n  const rows = response.rows ?? [];\n  const aggregates = response.aggregates ?? {};\n\n  if (relationshipName === undefined) {\n    switch (orderByElement.target.type) {\n      case 'column':\n        if (rows.length > 1)\n          throw new Error(\n            `Unexpected number of rows (${rows.length}) returned by order by element query`,\n          );\n\n        const fieldValue =\n          rows.length === 1\n            ? rows[0][getColumnSelector(orderByElement.target.column)]\n            : null;\n        if (fieldValue !== null && typeof fieldValue === 'object')\n          throw new Error(\n            'Column order by target path did not end in a column field value',\n          );\n\n        return coerceUndefinedToNull(fieldValue) as RawScalarValue;\n\n      case 'single_column_aggregate':\n        return aggregates[orderByElement.target.column];\n\n      case 'star_count_aggregate':","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/hasura/graphql-engine/blob/724551b9ae87845594ef0408cff0e50eb6c90dc5/dc-agents/reference/src/query.ts#L425-L461","documentation":"While extracting a sort key for an order-by element with an empty target_path, the engine issues a tiny query for the element's column value and expects at most one row back. Getting more than one row means the subquery produced a multi-row result, violating the single-value assumption of a column order-by on the current table.","triggerScenarios":"An order_by element whose target is type 'column' with no relationship path, where the generated subquery (which has no limit) returns 2+ rows — typically caused by a buggy subquery builder not applying the intended limit/where.","commonSituations":"Custom forks of the reference agent where the order-by subquery loses its where/limit clause; queries with malformed order_by target_path arrays that unexpectedly resolve to multi-row results.","solutions":["If building QueryRequest by hand, verify each order_by element has target_path: ['ColumnName'] for simple column sorts.","If maintaining a fork, ensure buildQueryForPathedOrderByElement-derived subqueries preserve limit/where so only one row returns.","File/inspect against the reference implementation to find where the subquery diverges from the single-row contract."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"for (const ob of queryRequest.order_by ?? []) {\n  if (ob.target_path.length === 0 && ob.target.type !== 'column') throw new Error('invalid order_by');\n}","typeGuard":"const isScalar = (v: unknown): v is RawScalarValue => v !== null && typeof v !== 'object';","tryCatchPattern":null,"preventionTips":["Build order_by via helpers that always set target_path to [columnName] for simple sorts","Never remove where/limit from order-by subqueries when forking the engine"],"tags":["hasura","data-connector","order-by","invariant-violation"],"backgroundTag":"unexpected-query-result-cardinality","analyzedSha":"724551b9ae87845594ef0408cff0e50eb6c90dc5","analyzedAt":"2026-08-28T07:32:55.105Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}