has_one and belongs_to

This commit is contained in:
Chris Tsang 2021-06-22 15:52:22 +08:00
parent a60c2da701
commit cbf16102c0
4 changed files with 14 additions and 7 deletions

View File

@ -28,18 +28,25 @@ pub trait EntityTrait: EntityName {
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Column = Self::Column>;
fn has_one<R>(related: R) -> RelationBuilder<Self, R>
fn belongs_to<R>(related: R) -> RelationBuilder<Self, R>
where
R: EntityTrait,
{
RelationBuilder::new(RelationType::HasOne, Self::default(), related)
}
fn has_one<R>(_: R) -> RelationBuilder<Self, R>
where
R: EntityTrait + Related<Self>,
{
RelationBuilder::from_rel(RelationType::HasOne, R::to().rev())
}
fn has_many<R>(_: R) -> RelationBuilder<Self, R>
where
R: EntityTrait + Related<Self>,
{
RelationBuilder::from_rel_def(R::to().rev())
RelationBuilder::from_rel(RelationType::HasMany, R::to().rev())
}
/// ```

View File

@ -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),

View File

@ -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(),

View File

@ -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(),