Skip to main content
Version: 0.4.x

Custom Joins

You can use the join method to construct complex join select query. It takes any RelationDef defined in entity file, you can define relation with 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(" ")