Skip to main content
Version: 0.5.x

Custom Select

By default, SeaORM will select all columns defined in the Column enum. You can override the defaults if you wish to select certain columns only.

Clear Default Selection

Clear the default selection by calling the select_only method if needed. Then, you can select some of the attributes or even custom expressions after it.

// Selecting all columns
r#"SELECT "cake"."id", "cake"."name" FROM "cake""#

Select Some Attributes Only

Use select_only and column methods together to select only the attributes you want.

// Selecting the name column only
r#"SELECT "cake"."name" FROM "cake""#

Select Custom Expressions

Select any custom expression with column_as method, it takes any sea_query::SimpleExpr and an alias. Use sea_query::Expr helper to build SimpleExpr.

use sea_query::{Alias, Expr};

.column_as(Expr::col(cake::Column::Id).max().sub(Expr::col(cake::Column::Id)), "id_diff")
.column_as(Expr::cust("CURRENT_TIMESTAMP"), "current_time")
r#"SELECT "cake"."id", "cake"."name", MAX("id") - "id" AS "id_diff", CURRENT_TIMESTAMP AS "current_time" FROM "cake""#

Handling Custom Selects

You can use a custom struct derived from the FromQueryResult trait to handle the result of a complex query. It is especially useful when dealing with custom columns or multiple joins which cannot directly be converted into models. It may be used to receive the result of any query, even raw SQL.

use sea_orm::{FromQueryResult, JoinType, RelationTrait};
use sea_query::Expr;

struct CakeAndFillingCount {
id: i32,
name: String,
count: i32,

let cake_counts: Vec<CakeAndFillingCount> = cake::Entity::find()
.column_as(filling::Column::Id.count(), "count")
// construct `RelationDef` on the fly
// reuse a `Relation` from existing Entity
.join(JoinType::InnerJoin, cake_filling::Relation::Filling.def())