hasura/graphql-engine · error · Error

Found a column field value in the middle of a order by targe

Error message

Found a column field value in the middle of a order by target path: ${orderByElement.target_path}

What it means

While walking a multi-segment order-by target_path, each intermediate segment must resolve to an object (nested row) so traversal can continue. If an intermediate value is null or a non-object, a column value appeared in the middle of the path, which is invalid — only the final path segment may be a column value.

Source

Thrown at dc-agents/reference/src/query.ts:475

      case 'single_column_aggregate':
        return aggregates[orderByElement.target.column];

      case 'star_count_aggregate':
        return aggregates['count'];

      default:
        return unreachable(orderByElement.target['type']);
    }
  } else {
    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][relationshipName] : null;
    if (fieldValue === null || typeof fieldValue !== 'object')
      throw new Error(
        `Found a column field value in the middle of a order by target path: ${orderByElement.target_path}`,
      );

    const innerOrderByElement = {
      ...orderByElement,
      target_path: remainingPath,
    };
    return extractResultFromOrderByElementQueryResponse(
      innerOrderByElement,
      fieldValue,
    );
  }
};

const makeGetOrderByElementValue =
  (
    findRelationship: (relationshipName: RelationshipName) => Relationship,
    applyRedaction: ApplyRedaction,

View on GitHub (pinned to 724551b9ae)

Solutions

  1. Ensure target_path starts with relationship names and only the last segment names the target column.
  2. If null related data is the cause, filter out rows with missing relationships before ordering or backfill the data.
  3. Validate the path against the schema's relationship names before sending the query.

Example fix

// before
order_by: [{ target_path: ['name', 'artist'], target: { type: 'column', column: 'name' }, order: 'asc' }]

// after
order_by: [{ target_path: ['artist', 'name'], target: { type: 'column', column: 'name' }, order: 'asc' }]
Defensive patterns

Strategy: validation

Validate before calling

const path = orderBy.target_path;
if (path.slice(0, -1).some((seg, i) => !relationshipNames.includes(seg)))
  throw new Error('intermediate order_by path segments must be relationships');

Type guard

const isObjectField = (v: unknown): v is Record<string, unknown> => v !== null && typeof v === 'object';

Prevention

When it happens

Trigger: An order_by with a target_path like ['a', 'b', 'c'] where traversing 'a' yields null or a scalar instead of a nested record — e.g. the first segment is a column name, or the relationship data is missing/null.

Common situations: Hand-written target_path arrays that start with a column instead of a relationship; null related rows (left-join style) in the static dataset; path segments that don't match any relationship name.

Related errors


AI-assisted analysis of hasura/graphql-engine@724551b9ae (2026-08-28). Data as JSON: /api/errors/9181ce6084901af5. Report an issue: GitHub.