hasura/graphql-engine · error · Error
Unexpected number of rows (${rows.length}) returned by order
Error message
Unexpected number of rows (${rows.length}) returned by order by element query What it means
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.
Source
Thrown at dc-agents/reference/src/query.ts:443
},
},
};
}
};
const extractResultFromOrderByElementQueryResponse = (
orderByElement: OrderByElement,
response: QueryResponse,
): RawScalarValue => {
const [relationshipName, ...remainingPath] = orderByElement.target_path;
const rows = response.rows ?? [];
const aggregates = response.aggregates ?? {};
if (relationshipName === undefined) {
switch (orderByElement.target.type) {
case 'column':
if (rows.length > 1)
throw new Error(
`Unexpected number of rows (${rows.length}) returned by order by element query`,
);
const fieldValue =
rows.length === 1
? rows[0][getColumnSelector(orderByElement.target.column)]
: null;
if (fieldValue !== null && typeof fieldValue === 'object')
throw new Error(
'Column order by target path did not end in a column field value',
);
return coerceUndefinedToNull(fieldValue) as RawScalarValue;
case 'single_column_aggregate':
return aggregates[orderByElement.target.column];
case 'star_count_aggregate':View on GitHub (pinned to 724551b9ae)
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.
Defensive patterns
Strategy: validation
Validate before calling
for (const ob of queryRequest.order_by ?? []) {
if (ob.target_path.length === 0 && ob.target.type !== 'column') throw new Error('invalid order_by');
} Type guard
const isScalar = (v: unknown): v is RawScalarValue => v !== null && typeof v !== 'object';
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Column order by target path did not end in a column field va
- Found a column field value in the middle of a order by targe
- Cannot perform an order by target of type ${orderByElement.t
- makeGetOrderByElementValue: Only table relationships current
- Cannot find database ${config.db}
AI-assisted analysis of hasura/graphql-engine@724551b9ae (2026-08-28).
Data as JSON: /api/errors/bc629731eb97411c.
Report an issue: GitHub.