From 835b2cd4bcf4eeb1fad30aca50e205ad587c1134 Mon Sep 17 00:00:00 2001 From: Dmitrii Aleksandrov Date: Fri, 8 Mar 2024 11:26:04 +0400 Subject: [PATCH 1/3] Add `RelationDef::from_alias()` --- src/entity/relation.rs | 47 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/entity/relation.rs b/src/entity/relation.rs index 6f0d105f77..c2a33c7883 100644 --- a/src/entity/relation.rs +++ b/src/entity/relation.rs @@ -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; @@ -167,6 +167,51 @@ 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::*, DbBackend, tests_cfg::{cake, cake_filling}}; + /// 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(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 From ccc408eec0af65cd91cdaa5c7a40bce7ca3a7ac6 Mon Sep 17 00:00:00 2001 From: Dmitrii Aleksandrov Date: Fri, 8 Mar 2024 11:35:43 +0400 Subject: [PATCH 2/3] Fix code formatting --- src/entity/relation.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/entity/relation.rs b/src/entity/relation.rs index c2a33c7883..98a87fbc4d 100644 --- a/src/entity/relation.rs +++ b/src/entity/relation.rs @@ -178,7 +178,12 @@ impl RelationDef { /// e.g. https://github.com/SeaQL/sea-orm/discussions/2133 /// /// ``` - /// use sea_orm::{entity::*, query::*, DbBackend, tests_cfg::{cake, cake_filling}}; + /// use sea_orm::{ + /// entity::*, + /// query::*, + /// DbBackend, + /// tests_cfg::{cake, cake_filling} + /// }; /// use sea_query::Alias; /// /// let cf = Alias::new("cf"); From f9f47d2a4b60b97517771d04be112ae51b9d9d06 Mon Sep 17 00:00:00 2001 From: Dmitrii Aleksandrov Date: Fri, 8 Mar 2024 11:38:26 +0400 Subject: [PATCH 3/3] Fix code formatting again --- src/entity/relation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entity/relation.rs b/src/entity/relation.rs index 98a87fbc4d..db995f8374 100644 --- a/src/entity/relation.rs +++ b/src/entity/relation.rs @@ -181,8 +181,8 @@ impl RelationDef { /// use sea_orm::{ /// entity::*, /// query::*, + /// tests_cfg::{cake, cake_filling}, /// DbBackend, - /// tests_cfg::{cake, cake_filling} /// }; /// use sea_query::Alias; ///