From cbf16102c01cf1e7bf1c9aed421084064c634cc6 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 22 Jun 2021 15:52:22 +0800 Subject: [PATCH] has_one and belongs_to --- src/entity/base_entity.rs | 11 +++++++++-- src/entity/relation.rs | 4 ++-- src/tests_cfg/cake_filling.rs | 4 ++-- src/tests_cfg/fruit.rs | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/entity/base_entity.rs b/src/entity/base_entity.rs index 8d143443..1553acd7 100644 --- a/src/entity/base_entity.rs +++ b/src/entity/base_entity.rs @@ -28,18 +28,25 @@ pub trait EntityTrait: EntityName { type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn; - fn has_one(related: R) -> RelationBuilder + fn belongs_to(related: R) -> RelationBuilder where R: EntityTrait, { RelationBuilder::new(RelationType::HasOne, Self::default(), related) } + fn has_one(_: R) -> RelationBuilder + where + R: EntityTrait + Related, + { + RelationBuilder::from_rel(RelationType::HasOne, R::to().rev()) + } + fn has_many(_: R) -> RelationBuilder where R: EntityTrait + Related, { - RelationBuilder::from_rel_def(R::to().rev()) + RelationBuilder::from_rel(RelationType::HasMany, R::to().rev()) } /// ``` diff --git a/src/entity/relation.rs b/src/entity/relation.rs index 6bd487d0..7c9213d3 100644 --- a/src/entity/relation.rs +++ b/src/entity/relation.rs @@ -78,10 +78,10 @@ where } } - pub(crate) fn from_rel_def(rel: RelationDef) -> Self { + pub(crate) fn from_rel(rel_type: RelationType, rel: RelationDef) -> Self { Self { entities: PhantomData, - rel_type: rel.rel_type, + rel_type, from_tbl: rel.from_tbl, to_tbl: rel.to_tbl, from_col: Some(rel.from_col), diff --git a/src/tests_cfg/cake_filling.rs b/src/tests_cfg/cake_filling.rs index fbd95a70..6b5c20aa 100644 --- a/src/tests_cfg/cake_filling.rs +++ b/src/tests_cfg/cake_filling.rs @@ -54,11 +54,11 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Cake => Entity::has_one(super::cake::Entity) + Self::Cake => Entity::belongs_to(super::cake::Entity) .from(Column::CakeId) .to(super::cake::Column::Id) .into(), - Self::Filling => Entity::has_one(super::filling::Entity) + Self::Filling => Entity::belongs_to(super::filling::Entity) .from(Column::FillingId) .to(super::filling::Column::Id) .into(), diff --git a/src/tests_cfg/fruit.rs b/src/tests_cfg/fruit.rs index a915e37a..0511ae58 100644 --- a/src/tests_cfg/fruit.rs +++ b/src/tests_cfg/fruit.rs @@ -55,7 +55,7 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Cake => Entity::has_one(super::cake::Entity) + Self::Cake => Entity::belongs_to(super::cake::Entity) .from(Column::CakeId) .to(super::cake::Column::Id) .into(),