Skip to main content
Version: 0.5.x

Custom Joins

You can use the join method to construct complex join select queries. It takes any RelationDef defined in entity files, and you can define relation with the belongs_to method as well. Join type is specified using JoinType such as inner join, left join and right join.

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

.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())
"SELECT `cake`.`id`, `cake`.`name`, COUNT(`filling`.`id`) AS `count` FROM `cake`",
"INNER JOIN `cake_filling` ON `cake_filling`.`cake_id` = `cake`.`id`",
"INNER JOIN `filling` ON `cake_filling`.`filling_id` = `filling`.`id`",
"GROUP BY `cake`.`id`",
"HAVING COUNT(`filling`.`id`) = 2",
.join(" ")

You can use a custom struct derived from the FromQueryResult trait to handle the result of such complex query. See here for details.