SeaQL/sea-orm · error

cannot apply alias for expr other than Column or AsEnum

Error message

cannot apply alias for expr other than Column or AsEnum

What it means

The catch-all arm of apply_alias's match panics with this message for any select expression that is neither a Column nor an AsEnum-wrapped Column. The combinator aliases every selected column by name, so non-column expressions are unsupported by design.

Source

Thrown at src/query/combine.rs:72

                    let col = match &sel.expr {
                        SimpleExpr::Column(col_ref) => match col_ref.column() {
                            Some(col) => col,
                            None => {
                                panic!("cannot apply alias for Column with asterisk");
                            }
                        },
                        SimpleExpr::AsEnum(_, simple_expr) => match simple_expr.as_ref() {
                            SimpleExpr::Column(col_ref) => match col_ref.column() {
                                Some(col) => col,
                                None => {
                                    panic!("cannot apply alias for AsEnum with asterisk")
                                }
                            },
                            _ => {
                                panic!("cannot apply alias for AsEnum with expr other than Column")
                            }
                        },
                        _ => panic!("cannot apply alias for expr other than Column or AsEnum"),
                    };
                    let alias = format!("{}{}", pre, col.to_string().as_str());
                    sel.alias = Some(alias.into_iden());
                }
            };
        });
        self
    }

    /// Selects extra Entity and returns it together with the Entity from `Self`
    pub fn select_also<F>(mut self, _: F) -> SelectTwo<E, F>
    where
        F: EntityTrait,
    {
        self = self.apply_alias(SelectA.as_str());
        SelectTwo::new(self.into_query())
    }

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Restrict the select list of these combinators to plain columns
  2. Use .expr_as(expr, alias) with explicit aliasing instead of the combinator's automatic aliasing
  3. Split computed fields into their own unaliased query and join results in application code

Example fix

// before
query.expr(Expr::col(user::COLUMN.age).add(1));
query.select_also(post::Entity);
// after
query.column(user::COLUMN.age);
let age_plus = Expr::col(user::COLUMN.age).add(1); // handled separately
Defensive patterns

Strategy: validation

Validate before calling

fn supports_auto_alias(expr: &SimpleExpr) -> bool {
    matches!(expr, SimpleExpr::Column(_) | SimpleExpr::AsEnum(_, _))
}

Type guard

fn is_column_like(expr: &SimpleExpr) -> bool {
    matches!(expr, SimpleExpr::Column(_) | SimpleExpr::AsEnum(_, _))
}

Try / catch

// pre-assert; panics cannot be caught in Rust
assert!(sel_exprs.iter().all(supports_auto_alias), "use explicit expr_as for non-column exprs");

Prevention

When it happens

Trigger: Calling select_also / select_with / select_two_required / left_join_linked on a query whose select list contains any non-column expression such as Function, Case, Binary, SubQuery or Keyword exprs.

Common situations: Aggregates (MAX, COUNT) or expressions (col + 1) added to a combined query; template-generated selects containing computed fields.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10). Data as JSON: /api/errors/642ce66da0688f32. Report an issue: GitHub.