{"record":{"id":"d6becda846e40633","repo":"SeaQL/sea-orm","slug":"cannot-apply-alias-for-expr-other-than-column-or-a","errorCode":null,"errorMessage":"cannot apply alias for expr other than Column or AsEnum","messagePattern":"cannot apply alias for expr other than Column or AsEnum","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sea-orm-sync/src/query/combine.rs","lineNumber":72,"sourceCode":"                    let col = match &sel.expr {\n                        SimpleExpr::Column(col_ref) => match col_ref.column() {\n                            Some(col) => col,\n                            None => {\n                                panic!(\"cannot apply alias for Column with asterisk\");\n                            }\n                        },\n                        SimpleExpr::AsEnum(_, simple_expr) => match simple_expr.as_ref() {\n                            SimpleExpr::Column(col_ref) => match col_ref.column() {\n                                Some(col) => col,\n                                None => {\n                                    panic!(\"cannot apply alias for AsEnum with asterisk\")\n                                }\n                            },\n                            _ => {\n                                panic!(\"cannot apply alias for AsEnum with expr other than Column\")\n                            }\n                        },\n                        _ => panic!(\"cannot apply alias for expr other than Column or AsEnum\"),\n                    };\n                    let alias = format!(\"{}{}\", pre, col.to_string().as_str());\n                    sel.alias = Some(alias.into_iden());\n                }\n            };\n        });\n        self\n    }\n\n    /// Selects extra Entity and returns it together with the Entity from `Self`\n    pub fn select_also<F>(mut self, _: F) -> SelectTwo<E, F>\n    where\n        F: EntityTrait,\n    {\n        self = self.apply_alias(SelectA.as_str());\n        SelectTwo::new(self.into_query())\n    }\n","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/SeaQL/sea-orm/blob/e29bcd1b417c41a553b386fe94511d7c64a1c8ec/sea-orm-sync/src/query/combine.rs#L54-L90","documentation":"This is the fall-through panic of `apply_alias`: any selected expression that is neither `SimpleExpr::Column` nor `SimpleExpr::AsEnum(Column)` cannot be given the table-prefix alias that `select_also`/`select_with`/`select_two_required`/`left_join_linked` require, so the library panics. It exists to keep generated SELECT clause unambiguous when merging two entities into one row.","triggerScenarios":"Calling `select_also`, `select_also_fake`, `select_with`, `select_two_required`, or `left_join_linked` on a query whose select list contains expressions like raw `Expr` values, functions (e.g. `count`, `max`), subqueries, tuples, or values added via `.expr(...)`/`.expr_as(...)`/`.column_as(...)` with a non-column expression.","commonSituations":"Adding an aggregate (`count`, `sum`) or a computed expression to a find() query and then calling `select_also(other_entity)`; passing a subquery-built select to a combined query; copying SQL fragments into the select list before joining two entities.","solutions":["Replace the arbitrary expression with a plain column before calling select_also/select_with; compute the value in Rust after fetching instead.","If you need the computed value, use `.column_as(expr, \"explicit_alias\")` and read it from the raw query result (`JsonRow`/`from_raw_sql`) instead of into_model on a combined select.","Run two queries: one plain find() for the entity, a separate query for the aggregate/expression, and merge results in application code.","Restructure so the expression belongs to a `into_model` on a single-entity select rather than an aliased combined select."],"exampleFix":"// before\nlet rows = cake::Entity::find()\n    .expr_as(Expr::col(cake::COLUMN.id).count(), \"cake_count\") // non-column expr\n    .select_also(fruit::Entity)\n    .into_model::<CakeFruit>()\n    .one(db).await?;\n\n// after: plain columns for the combined query\nlet rows = cake::Entity::find()\n    .column(cake::COLUMN.id)\n    .column(cake::COLUMN.name)\n    .join(JoinType::LeftJoin, fruit::Relation::Cake.def())\n    .select_also(fruit::Entity)\n    .into_model::<CakeFruit>()\n    .one(db).await?;","handlingStrategy":"validation","validationCode":"// Allow only plain columns (and enum-over-column) through combined selects\nfn aliasable(expr: &SimpleExpr) -> bool {\n    matches!(expr, SimpleExpr::Column(_))\n        || matches!(expr, SimpleExpr::AsEnum(_, inner) if matches!(inner.as_ref(), SimpleExpr::Column(_)))\n}","typeGuard":"fn is_simple_column(expr: &SimpleExpr) -> bool {\n    matches!(expr, SimpleExpr::Column(_))\n}","tryCatchPattern":null,"preventionTips":["Don't add aggregates, functions, subqueries, or raw expressions to queries that will be used with select_also/select_with/select_two_required.","Compute aggregates in a separate query and merge in application code.","If a computed column is unavoidable, alias it explicitly and consume the result via `into_json`/raw rows rather than the combined-select model path."],"tags":["panic","seaorm","query-builder","select-alias","expression"],"backgroundTag":"unsupported-operation","analyzedSha":"e29bcd1b417c41a553b386fe94511d7c64a1c8ec","analyzedAt":"2026-09-10T11:31:52.468Z","contentChangedAt":"2026-09-10T11:31:52.468Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}