Skip to content

Commit 10618e0

Browse files
committed
OR condition relation SeaQL/sea-orm#1433
1 parent e11d115 commit 10618e0

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

SeaORM/docs/0.12.x-CHANGELOG_temp.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ impl MigratorTrait for Migrator {
4343
}
4444
}
4545
```
46-
================================ All Changes above was being Documented ================================
4746
* Added option to construct chained AND / OR join on condition https://github.com/SeaQL/sea-orm/pull/1433
4847
```rs
4948
use sea_orm::entity::prelude::*;
@@ -110,6 +109,7 @@ assert_eq!(
110109
.join(" ")
111110
);
112111
```
112+
================================ All Changes above was being Documented ================================
113113
* Supports entity with composite primary key of length 12 https://github.com/SeaQL/sea-orm/pull/1508
114114
* Implemented `IntoIdentity` for `Identity` https://github.com/SeaQL/sea-orm/pull/1508
115115
* `Identity` supports up to identity tuple of `DynIden` with length up to 12 https://github.com/SeaQL/sea-orm/pull/1508

SeaORM/docs/06-relation/06-custom-join-condition.md

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ It can be done via one of the following ways.
1717
## Relation
1818

1919
Add your additional join conditions directly to the relation enum. The easiest way is to provide a `sea_query::SimpleExpr` via `on_condition` procedural macros attribute.
20+
If you want to have a `OR` condition relation, you can use `condition_type = "any"` to alter the relation.
2021

2122
```rust
2223
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
@@ -29,6 +30,13 @@ pub enum Relation {
2930
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"#
3031
)]
3132
TropicalFruit,
33+
// specify `condition_type = "any"` to override it
34+
#[sea_orm(
35+
has_many = "super::fruit::Entity",
36+
on_condition = r#"super::fruit::Column::Name.like("%tropical%")"#
37+
condition_type = "any",
38+
)]
39+
OrTropicalFruit,
3240
}
3341
```
3442

@@ -127,6 +135,7 @@ assert_eq!(
127135
## Custom Join
128136

129137
Last but not least, custom join conditions can be defined at the point you construct the join expression.
138+
Overriding condition relation can also be done in custom join on the fly.
130139

131140
```rust
132141
assert_eq!(
@@ -153,6 +162,19 @@ assert_eq!(
153162
.into_condition()
154163
})
155164
)
165+
.join_as_rev(
166+
JoinType::LeftJoin,
167+
cake_filling::Relation::Cake
168+
.def()
169+
// chained AND / OR join on condition
170+
.condition_type(ConditionType::Any)
171+
.on_condition(|left, _right| {
172+
Expr::col((left, cake_filling::Column::CakeId))
173+
.gt(10)
174+
.into_condition()
175+
}),
176+
Alias::new("cake_filling_alias")
177+
)
156178
.join(JoinType::LeftJoin, filling::Relation::Vendor.def())
157179
.build(DbBackend::MySql)
158180
.to_string(),
@@ -161,6 +183,7 @@ assert_eq!(
161183
"LEFT JOIN `fruit` ON `cake`.`id` = `fruit`.`cake_id` AND `fruit`.`name` LIKE '%tropical%'",
162184
"LEFT JOIN `cake_filling` ON `cake`.`id` = `cake_filling`.`cake_id` AND `cake_filling`.`cake_id` > 10",
163185
"LEFT JOIN `filling` ON `cake_filling`.`filling_id` = `filling`.`id` AND `filling`.`name` LIKE '%lemon%'",
186+
"LEFT JOIN `cake_filling` AS `cake_filling_alias` ON `cake_filling_alias`.`cake_id` = `cake`.`id` OR `cake_filling_alias`.`cake_id` > 10",
164187
"LEFT JOIN `vendor` ON `filling`.`vendor_id` = `vendor`.`id`",
165188
]
166189
.join(" ")

0 commit comments

Comments
 (0)