Has many relation to be auto defined
This commit is contained in:
parent
72408eedfc
commit
d77a7c8ab4
@ -1,7 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ActiveModelTrait, ColumnTrait, Delete, DeleteOne, FromQueryResult, Insert, ModelTrait,
|
ActiveModelTrait, ColumnTrait, Delete, DeleteOne, FromQueryResult, Insert, ModelTrait,
|
||||||
OneOrManyActiveModel, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder,
|
OneOrManyActiveModel, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, Related,
|
||||||
RelationTrait, RelationType, Select, Update, UpdateOne,
|
RelationBuilder, RelationTrait, RelationType, Select, Update, UpdateOne,
|
||||||
};
|
};
|
||||||
use sea_query::{Iden, IntoValueTuple};
|
use sea_query::{Iden, IntoValueTuple};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -35,11 +35,11 @@ pub trait EntityTrait: EntityName {
|
|||||||
RelationBuilder::new(RelationType::HasOne, Self::default(), related)
|
RelationBuilder::new(RelationType::HasOne, Self::default(), related)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_many<R>(related: R) -> RelationBuilder<Self, R>
|
fn has_many<R>(_: R) -> RelationBuilder<Self, R>
|
||||||
where
|
where
|
||||||
R: EntityTrait,
|
R: EntityTrait + Related<Self>,
|
||||||
{
|
{
|
||||||
RelationBuilder::new(RelationType::HasMany, Self::default(), related)
|
RelationBuilder::from_rel_def(R::to().rev())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -78,6 +78,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn from_rel_def(rel: RelationDef) -> Self {
|
||||||
|
Self {
|
||||||
|
entities: PhantomData,
|
||||||
|
rel_type: rel.rel_type,
|
||||||
|
from_tbl: rel.from_tbl,
|
||||||
|
to_tbl: rel.to_tbl,
|
||||||
|
from_col: Some(rel.from_col),
|
||||||
|
to_col: Some(rel.to_col),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from(mut self, identifier: E::Column) -> Self {
|
pub fn from(mut self, identifier: E::Column) -> Self {
|
||||||
self.from_col = Some(identifier.into_identity());
|
self.from_col = Some(identifier.into_identity());
|
||||||
self
|
self
|
||||||
|
@ -52,10 +52,7 @@ impl ColumnTrait for Column {
|
|||||||
impl RelationTrait for Relation {
|
impl RelationTrait for Relation {
|
||||||
fn def(&self) -> RelationDef {
|
fn def(&self) -> RelationDef {
|
||||||
match self {
|
match self {
|
||||||
Self::Fruit => Entity::has_many(super::fruit::Entity)
|
Self::Fruit => Entity::has_many(super::fruit::Entity).into(),
|
||||||
.from(Column::Id)
|
|
||||||
.to(super::fruit::Column::CakeId)
|
|
||||||
.into(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,9 @@ impl PrimaryKeyTrait for PrimaryKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||||
pub enum Relation {}
|
pub enum Relation {
|
||||||
|
Cake,
|
||||||
|
}
|
||||||
|
|
||||||
impl ColumnTrait for Column {
|
impl ColumnTrait for Column {
|
||||||
type EntityName = Entity;
|
type EntityName = Entity;
|
||||||
@ -52,7 +54,18 @@ impl ColumnTrait for Column {
|
|||||||
|
|
||||||
impl RelationTrait for Relation {
|
impl RelationTrait for Relation {
|
||||||
fn def(&self) -> RelationDef {
|
fn def(&self) -> RelationDef {
|
||||||
panic!()
|
match self {
|
||||||
|
Self::Cake => Entity::has_one(super::cake::Entity)
|
||||||
|
.from(Column::CakeId)
|
||||||
|
.to(super::cake::Column::Id)
|
||||||
|
.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::cake::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Cake.def()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user