Optional: Building SQL Queries with SeaQuery

If you prefer the flexibility of SQL, you can use SeaQuery to build SQL-like statements for any queries or operations.

SeaQuery is built-in for SeaORM, so no extra setup is required.

Insert statements

Raw SQL:

INSERT INTO `bakery` (`name`, `profit_margin`) VALUES ('SQL Bakery', -100)


fn main() {
use sea_query::{Alias, Query};

let columns: Vec<Alias> = ["name", "profit_margin"]

let mut stmt = Query::insert();

// Invoke `values_panic()` for each row
stmt.values_panic(["SQL Bakery".into(), (-100.0).into()]);

let builder = db.get_database_backend();

Select statements

Raw SQL:

SELECT `baker`.`name` FROM `baker` JOIN `bakery` ON `baker`.`bakery_id` = `bakery`.`id` ORDER BY `baker`.`name` ASC


If you are only interested in some of the columns, define a struct to hold the query result. It has to derive from the trait FromQueryResult.

If all columns are of interest, then the generated Model structs (e.g. baker::Model) can be used.

The fields of the struct must match the column names of the query result.

fn main() {
use sea_query::{Alias, Expr, JoinType, Order, Query};

struct BakerNameResult {
    name: String,


let column = (baker::Entity, Alias::new("name"));

let mut stmt = Query::select();
stmt.column(column.clone()) // Use `expr_as` instead of `column` if renaming is necessary
        Expr::tbl(baker::Entity, Alias::new("bakery_id"))
            .equals(bakery::Entity, Alias::new("id")),
    .order_by(column, Order::Asc);

let builder = db.get_database_backend();
let baker = BakerNameResult::find_by_statement(builder.build(&stmt))

let baker_names = baker.into_iter().map(|b| b.name).collect::<Vec<_>>();

    vec!["Charles", "Frederic", "Jolie", "Madeleine"]

Testing and Debugging

It's often useful to check the raw SQL of the SeaQuery-generated statements.

Use stmt.to_string(query_builder) to do that.

fn main() {
// Check the raw SQL of `stmt` in MySQL syntax
println!({}, stmt.to_string(MysqlQueryBuilder));