has_one and belongs_to
This commit is contained in:
parent
a60c2da701
commit
cbf16102c0
@ -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())
|
||||
}
|
||||
|
||||
/// ```
|
||||
|
@ -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),
|
||||
|
@ -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(),
|
||||
|
@ -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(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user