{"record":{"id":"1f5c714aa51c46eb","repo":"hasura/graphql-engine","slug":"cannot-order-by-array-relationship-relationship-n","errorCode":null,"errorMessage":"Cannot order by array relationship {relationship_name} in order by expression {order_by_expression}","messagePattern":"Cannot order by array relationship (.+?) in order by expression (.+?)","errorType":"validation","errorClass":"OrderByExpressionIssue","httpStatus":null,"severity":"error","filePath":"v3/crates/metadata-resolve/src/stages/order_by_expressions/types.rs","lineNumber":111,"sourceCode":"pub struct OrderableRelationship {\n    /// order_by_expression is optional.\n    /// If not present we will use order_by_expression from the model\n    /// that the relationship targets.\n    pub order_by_expression: Option<Qualified<OrderByExpressionName>>,\n}\n\n#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]\npub struct OrderByExpressionGraphqlConfig {\n    pub expression_type_name: ast::TypeName,\n}\n\n#[derive(Debug, thiserror::Error)]\npub enum OrderByExpressionIssue {\n    #[error(\"Duplicate order by expression found: {order_by_expression}\")]\n    DuplicateOrderByExpression {\n        order_by_expression: Qualified<OrderByExpressionIdentifier>,\n    },\n    #[error(\n        \"Cannot order by array relationship {relationship_name} in order by expression {order_by_expression}\"\n    )]\n    CannotOrderByAnArrayRelationship {\n        order_by_expression: Qualified<OrderByExpressionIdentifier>,\n        relationship_name: RelationshipName,\n    },\n    #[error(\n        \"The orderable field \\\"{field_name}\\\" has field arguments and cannot be used in order by expressions.\"\n    )]\n    OrderByFieldWithFieldArguments { field_name: FieldName },\n}\n\nimpl ShouldBeAnError for OrderByExpressionIssue {\n    fn should_be_an_error(&self, flags: &open_dds::flags::OpenDdFlags) -> bool {\n        match self {\n            OrderByExpressionIssue::DuplicateOrderByExpression { .. } => flags\n                .contains(open_dds::flags::Flag::DisallowDuplicateNamesAcrossTypesAndExpressions),\n            OrderByExpressionIssue::CannotOrderByAnArrayRelationship { .. } => {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/hasura/graphql-engine/blob/724551b9ae87845594ef0408cff0e50eb6c90dc5/v3/crates/metadata-resolve/src/stages/order_by_expressions/types.rs#L93-L129","documentation":"Thrown when an order-by expression tries to order by an array (many) relationship. Sorting by a one-to-many relationship is ambiguous (which element of the array determines the order?), so only object (single) relationships may be used in order-by expressions.","triggerScenarios":"Referencing an array/many relationship inside an order_by_expressions entry during resolution; the relationship's cardinality is Array, which the resolver rejects with CannotOrderByAnArrayRelationship.","commonSituations":"Trying to order posts by their comments, or a type by any of its one-to-many relations, in metadata; mis-declaring a relationship's kind as Array when it should be Object.","solutions":["Check the relationship named in the error: if it is genuinely one-to-many, it cannot be used for ordering — remove it from the expression","If the relationship should be single-object, fix its kind/type in the relationship definition","Order by a field of the related type via an object relationship instead"],"exampleFix":"# before\norder_by:\n  - relationship: comments   # array relationship\n    field: createdAt\n# after\norder_by:\n  - relationship: author     # object relationship\n    field: name","handlingStrategy":"validation","validationCode":"const rel = relationships[expr.relationship_name];\nif (rel?.kind === 'ArrayRelationship') {\n  throw new Error('cannot order by array relationship');\n}","typeGuard":"const isObjectRelationship = (r) => r?.kind === 'ObjectRelationship';","tryCatchPattern":null,"preventionTips":["Only reference ObjectRelationships in order-by expressions","Double-check relationship kind when creating orderable relationships"],"tags":["metadata","order-by","array-relationship","validation","open-dds"],"backgroundTag":"order-by-array-relationship","analyzedSha":"724551b9ae87845594ef0408cff0e50eb6c90dc5","analyzedAt":"2026-08-28T07:32:55.105Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}