Skip to main content

Module query

Module query 

Source
Expand description

Query builders used to express SELECT, INSERT, UPDATE, and DELETE statements against an Entity.

Most queries originate from an entity:

ⓘ
cake::Entity::find()        // -> Select<cake::Entity>
cake::Entity::insert(am)    // -> Insert<cake::ActiveModel>
cake::Entity::update(am)    // -> UpdateOne<cake::ActiveModel>
cake::Entity::delete(am)    // -> DeleteOne<cake::Entity>

The builders implement composable traits — QueryFilter, QuerySelect, QueryOrder, QueryTrait — for adding WHERE clauses, joins, ordering, and column projections. Once composed, hand the query to a ConnectionTrait via .one(db), .all(db), .exec(db), .stream(db), etc.

For raw SQL, use Statement together with the raw_sql! macro.

Re-exports§

pub use crate::ConnectionTrait;
pub use crate::CursorTrait;
pub use crate::InsertResult;
pub use crate::PaginatorTrait;
pub use crate::SelectExt;
pub use crate::Statement;
pub use crate::TransactionTrait;
pub use crate::UpdateResult;
pub use crate::StreamTrait;stream

Macros§

debug_query
Helper to get a raw SQL string from an object that impl QueryTrait.
debug_query_stmt
Helper to get a Statement from an object that impl QueryTrait.

Structs§

Condition
Represents the value of an Condition::any or Condition::all: a set of disjunctive or conjunctive conditions.
DebugQuery
Glue type used by the debug_query! / debug_query_stmt! macros to render a query for a specific backend or connection. Most code should use those macros rather than naming this type directly.
Delete
Type-level entry point for DELETE builders, e.g. Delete::one(model) and Delete::many(Entity). You normally call EntityTrait::delete / delete_many instead.
DeleteMany
Multi-row DELETE builder, returned by EntityTrait::delete_many. Add .filter(...) to scope which rows are deleted.
DeleteOne
A request to delete an ActiveModel.
DynIden
A prepared (quoted) identifier string.
Insert
Single-row INSERT builder, returned by EntityTrait::insert. Chain .on_conflict(...) or .on_empty_returning(...) then dispatch with .exec(db) or .exec_with_returning(db).
InsertMany
Multi-row INSERT builder, returned by EntityTrait::insert_many. Adds .exec_with_returning_many and .exec_with_returning_keys for batch inserts that need the resulting rows or primary keys.
Select
A SELECT query against entity E. Returned by EntityTrait::find; chain filters, joins, ordering, and projections onto it, then run it on a ConnectionTrait with .one(db) / .all(db) / .stream(db) / .paginate(db, n).
SelectA
Iden used to alias one side of a multi-table SELECT, so column names from each joined table can be prefixed without clashing (e.g. select_a_id, select_b_id).
SelectB
Iden used to alias one side of a multi-table SELECT, so column names from each joined table can be prefixed without clashing (e.g. select_a_id, select_b_id).
SelectC
Iden used to alias one side of a multi-table SELECT, so column names from each joined table can be prefixed without clashing (e.g. select_a_id, select_b_id).
SelectFive
Five-way join select.
SelectFour
Four-way join select.
SelectFourMany
Like SelectFour, but results are consolidated under the left model.
SelectSix
Six-way join select.
SelectThree
Three-way join select, yielding (E::Model, Option<F::Model>, Option<G::Model>). The TOP parameter is the join Topology.
SelectThreeMany
Like SelectThree, but results are consolidated under the left model: (E::Model, Vec<F::Model>, Vec<G::Model>).
SelectTwo
A SELECT joining two entities, yielding (E::Model, Option<F::Model>) per row — the right side is None for outer-join rows with no match. Returned by Select::find_also_related and similar helpers.
SelectTwoMany
A SELECT joining two entities, with results grouped into (E::Model, Vec<F::Model>) per left model. Returned by Select::find_with_related.
SelectTwoRequired
A SELECT joining two entities where both sides are required, yielding (E::Model, F::Model) per row (no Option).
TopologyChain
Chain join: each entity joined to the previous one in sequence.
TopologyStar
Star join: a centre entity joined separately to each of the others.
TryInsert
Wrapper of Insert / InsertMany, treats “no row inserted/id returned” as a normal outcome.
Update
Type-level entry point for UPDATE builders, e.g. Update::one(model) and Update::many(Entity). You normally call EntityTrait::update / update_many instead.
UpdateMany
Multi-row UPDATE builder, returned by EntityTrait::update_many. Add .col_expr(...) to set columns and .filter(...) to scope which rows are updated.
UpdateOne
A request to update an ActiveModel.
ValidatedDeleteOne
A validated DeleteOne request, where the primary key is set and it’s possible to generate the right SQL condition.
ValidatedUpdateOne
A validated UpdateOne request, where the primary key is set and it’s possible to generate the right SQL condition.
Values

Enums§

JoinType
Join types
JsonValue
Represents any valid JSON value.
Order
Ordering options
Value
Value variants

Traits§

ColumnAsExpr
Like IntoSimpleExpr but applies SeaORM’s enum-to-text cast for ActiveEnum columns appearing in a SELECT list.
ConditionalStatement
EntityOrSelect
Either an Entity or a Select<Entity>; accepted by LoaderTrait so you can pass the entity directly when no filtering is needed, or a pre-filtered Select when it is.
ExprTrait
“Operator” methods for building expressions.
IntoSimpleExpr
Conversion into a SimpleExpr. Implemented for entity columns so they can be used anywhere a sea_query expression is expected.
LoaderTrait
Batch-load related entities for a slice of parent models, avoiding the N+1 query problem.
OrderedStatement
QueryFilter
Methods for adding WHERE / AND / OR conditions to a query. The entry point most code uses is filter.
QueryOrder
Methods for adding ORDER BY clauses to a query.
QuerySelect
Methods for narrowing a query’s projection, joining other tables, and adding GROUP BY / HAVING clauses. Implemented for Select, SelectTwo, SelectTwoMany, and the higher-arity selects.
QueryTrait
Common operations on a SeaORM query builder: borrow the underlying sea_query statement, build it into a backend-specific Statement, or apply optional modifications via apply_if.
Topology
Marker trait describing how 3+ tables are joined: from one centre entity to several siblings (TopologyStar) or in a sequential chain (TopologyChain).