Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RelationDef::from_alias() #2146

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/entity/relation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{unpack_table_ref, EntityTrait, Identity, IdentityOf, Iterable, QuerySelect, Select};
use core::marker::PhantomData;
use sea_query::{
Alias, Condition, ConditionType, DynIden, ForeignKeyCreateStatement, JoinType, SeaRc,
Alias, Condition, ConditionType, DynIden, ForeignKeyCreateStatement, IntoIden, JoinType, SeaRc,
TableForeignKey, TableRef,
};
use std::fmt::Debug;
Expand Down Expand Up @@ -167,6 +167,56 @@ impl RelationDef {
}
}

/// Express the relation from a table alias.
///
/// This is a shorter and more discoverable equivalent to modifying `from_tbl` field by hand.
///
/// # Examples
///
/// Here's a short synthetic example.
/// In real life you'd use aliases when the table name comes up twice and you need to disambiguate,
/// e.g. https://github.com/SeaQL/sea-orm/discussions/2133
///
/// ```
/// use sea_orm::{
/// entity::*,
/// query::*,
/// tests_cfg::{cake, cake_filling},
/// DbBackend,
/// };
/// use sea_query::Alias;
///
/// let cf = Alias::new("cf");
///
/// assert_eq!(
/// cake::Entity::find()
/// .join_as(
/// JoinType::LeftJoin,
/// cake_filling::Relation::Cake.def().rev(),
/// cf.clone()
/// )
/// .join(
/// JoinType::LeftJoin,
/// cake_filling::Relation::Filling.def().from_alias(cf)
/// )
/// .build(DbBackend::MySql)
/// .to_string(),
/// [
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake`",
/// "LEFT JOIN `cake_filling` AS `cf` ON `cake`.`id` = `cf`.`cake_id`",
/// "LEFT JOIN `filling` ON `cf`.`filling_id` = `filling`.`id`",
/// ]
/// .join(" ")
/// );
/// ```
pub fn from_alias<A>(mut self, alias: A) -> Self
where
A: IntoIden,
{
self.from_tbl = self.from_tbl.alias(alias);
self
}

/// Set custom join ON condition.
///
/// This method takes a closure with two parameters
Expand Down
Loading