SeaQL/sea-orm · error
cannot apply alias for AsEnum with asterisk
Error message
cannot apply alias for AsEnum with asterisk
What it means
In `apply_alias`, when a selected expression is wrapped as `SimpleExpr::AsEnum`, the code unwraps it to an inner `SimpleExpr::Column` to derive the column name for the prefixed alias. If the inner column reference is an asterisk (wildcard, `col_ref.column()` returns None), no column name exists to build an alias from, so the library panics rather than emit a corrupt `SELECT * AS alias_x` clause. This is a deliberate internal invariant violation guard.
Source
Thrown at sea-orm-sync/src/query/combine.rs:65
self.query().exprs_mut_for_each(|sel| {
match &sel.alias {
Some(alias) => {
let alias = format!("{}{}", pre, alias.to_string().as_str());
sel.alias = Some(alias.into_iden());
}
None => {
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>View on GitHub (pinned to e29bcd1b41)
Solutions
- Select the enum column explicitly instead of using the asterisk/wildcard: use `.column(entity::COLUMN.my_enum_col)` so the AsEnum wraps a named Column.
- Remove the wildcard select from the query; enumerate the needed columns so each AsEnum expression resolves to a concrete column.
- If the wildcard comes from a helper building the select, change it to skip AsEnum-wrapped expressions or expand them per-column before calling select_also/select_with.
- If you believe wildcard + enum selection should be supported, file an issue; as a workaround apply the enum conversion after fetching the raw column instead of via AsEnum in the select.
Example fix
// before
let rows = cake::Entity::find()
.join(JoinType::LeftJoin, fruit::Relation::Cake.def())
.select_also(fruit::Entity) // aliasing pass sees AsEnum over asterisk
.into_model::<CakeFruit>()
.one(db).await?;
// after
let rows = cake::Entity::find()
.column_as(cake::COLUMN.id, "cake_id")
.column(cake::COLUMN.name) // explicit enum (AsEnum) columns, not wildcard
.join(JoinType::LeftJoin, fruit::Relation::Cake.def())
.select_also(fruit::Entity)
.into_model::<CakeFruit>()
.one(db).await?; Defensive patterns
Strategy: validation
Validate before calling
// Ensure every AsEnum-wrapped select item targets a named column before combined select
fn assert_no_wildcard_enum_select<E: EntityTrait>(sel: &Select<E>) -> bool { sel.query().distinct().is_ok() } // build selects explicitly; prefer:
// prefer: only pass enums via .column(entity::COLUMN.col) — never .* in select_also/select_with
true Type guard
fn is_named_column_select(expr: &SimpleExpr) -> bool {
match expr {
SimpleExpr::Column(c) => c.column().is_some(),
SimpleExpr::AsEnum(_, inner) => matches!(inner.as_ref(), SimpleExpr::Column(c) if c.column().is_some()),
_ => false,
}
} Prevention
- Never select table wildcards (`.column(Expr::col(...).asterisk())`) in queries passed to select_also/select_with/left_join_linked.
- List enum columns explicitly with `entity::COLUMN.name` so AsEnum always wraps a named Column.
- Add a debug-mode unit test that builds each combined query once; the panic fires at query build time, so test-builds catch it early.
When it happens
Trigger: Calling `select_also`, `select_also_fake`, `select_with`, `select_two_required`, or `left_join_linked` on a SelectTwo/SelectThree whose selected column list contains a `SimpleExpr::AsEnum` wrapping a wildcard/asterisk column expression (e.g. a `DeriveValueType` enum column selected via `Expr::col(...).into_column()` with Asterisk, or selecting all columns where one is an enum wrapper that got AsEnum-wrapped as asterisk).
Common situations: Selecting a whole table's columns (`.*`) in a combined select while at least one column is a SeaORM enum (DeriveValueType/DeriveActiveEnum) column, so the aliasing pass encounters `AsEnum(Asterisk)`. Typically appears when refactoring a join query to `select_also`/`select_with` without listing explicit columns.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- cannot apply alias for AsEnum with expr other than Column
- cannot apply alias for expr other than Column or AsEnum
- Not mock connection
- Not proxy connection
- cannot apply alias for Column with asterisk
AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10).
Data as JSON: /api/errors/ca3b3ab5f36979b9.
Report an issue: GitHub.