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

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.

Source

Thrown at sea-orm-sync/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. Replace the arbitrary expression with a plain column before calling select_also/select_with; compute the value in Rust after fetching instead.
  2. 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.
  3. Run two queries: one plain find() for the entity, a separate query for the aggregate/expression, and merge results in application code.
  4. Restructure so the expression belongs to a `into_model` on a single-entity select rather than an aliased combined select.

Example fix

// before
let rows = cake::Entity::find()
    .expr_as(Expr::col(cake::COLUMN.id).count(), "cake_count") // non-column expr
    .select_also(fruit::Entity)
    .into_model::<CakeFruit>()
    .one(db).await?;

// after: plain columns for the combined query
let rows = cake::Entity::find()
    .column(cake::COLUMN.id)
    .column(cake::COLUMN.name)
    .join(JoinType::LeftJoin, fruit::Relation::Cake.def())
    .select_also(fruit::Entity)
    .into_model::<CakeFruit>()
    .one(db).await?;
Defensive patterns

Strategy: validation

Validate before calling

// Allow only plain columns (and enum-over-column) through combined selects
fn aliasable(expr: &SimpleExpr) -> bool {
    matches!(expr, SimpleExpr::Column(_))
        || matches!(expr, SimpleExpr::AsEnum(_, inner) if matches!(inner.as_ref(), SimpleExpr::Column(_)))
}

Type guard

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

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/d6becda846e40633. Report an issue: GitHub.