Many to many by reverse relation
This commit is contained in:
parent
13bad8eb17
commit
8886269616
@ -24,7 +24,6 @@ pub enum PrimaryKey {
|
|||||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
Fruit,
|
Fruit,
|
||||||
CakeFilling,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ColumnTrait for Column {
|
impl ColumnTrait for Column {
|
||||||
@ -45,10 +44,6 @@ impl RelationTrait for Relation {
|
|||||||
.from(Column::Id)
|
.from(Column::Id)
|
||||||
.to(super::fruit::Column::CakeId)
|
.to(super::fruit::Column::CakeId)
|
||||||
.into(),
|
.into(),
|
||||||
Self::CakeFilling => Entity::has_many(super::cake_filling::Entity)
|
|
||||||
.from(Column::Id)
|
|
||||||
.to(super::cake_filling::Column::CakeId)
|
|
||||||
.into(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,7 +60,7 @@ impl Related<super::filling::Entity> for Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn via() -> Option<RelationDef> {
|
fn via() -> Option<RelationDef> {
|
||||||
Some(Relation::CakeFilling.def())
|
Some(super::cake_filling::Relation::Cake.def().rev())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,4 +68,8 @@ impl Model {
|
|||||||
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
|
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
|
||||||
Entity::find_related().belongs_to::<Entity>(self)
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_filling(&self) -> Select<super::filling::Entity> {
|
||||||
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,11 +42,11 @@ 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::Cake => Entity::has_many(super::cake::Entity)
|
Self::Cake => Entity::has_one(super::cake::Entity)
|
||||||
.from(Column::CakeId)
|
.from(Column::CakeId)
|
||||||
.to(super::cake::Column::Id)
|
.to(super::cake::Column::Id)
|
||||||
.into(),
|
.into(),
|
||||||
Self::Filling => Entity::has_many(super::filling::Entity)
|
Self::Filling => Entity::has_one(super::filling::Entity)
|
||||||
.from(Column::FillingId)
|
.from(Column::FillingId)
|
||||||
.to(super::filling::Column::Id)
|
.to(super::filling::Column::Id)
|
||||||
.into(),
|
.into(),
|
||||||
|
@ -40,3 +40,19 @@ impl RelationTrait for Relation {
|
|||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Related<super::cake::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::cake_filling::Relation::Cake.def()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::cake_filling::Relation::Filling.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Model {
|
||||||
|
pub fn find_cake(&self) -> Select<super::cake::Entity> {
|
||||||
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -150,5 +150,27 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
|
|||||||
println!("{:?}\n", bb);
|
println!("{:?}\n", bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print!("find fillings for cheese cake: ");
|
||||||
|
|
||||||
|
let cheese = cake::Entity::find_by(1).one(db).await?;
|
||||||
|
|
||||||
|
let fillings: Vec<filling::Model> = cheese.find_filling().all(db).await?;
|
||||||
|
|
||||||
|
println!();
|
||||||
|
for ff in fillings.iter() {
|
||||||
|
println!("{:?}\n", ff);
|
||||||
|
}
|
||||||
|
|
||||||
|
print!("find cakes for lemon: ");
|
||||||
|
|
||||||
|
let lemon = filling::Entity::find_by(2).one(db).await?;
|
||||||
|
|
||||||
|
let cakes: Vec<cake::Model> = lemon.find_cake().all(db).await?;
|
||||||
|
|
||||||
|
println!();
|
||||||
|
for cc in cakes.iter() {
|
||||||
|
println!("{:?}\n", cc);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,19 @@ where
|
|||||||
to_col: Option<Identity>,
|
to_col: Option<Identity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RelationDef {
|
||||||
|
/// Reverse this relation (swap from and to)
|
||||||
|
pub fn rev(self) -> Self {
|
||||||
|
Self {
|
||||||
|
rel_type: self.rel_type,
|
||||||
|
from_tbl: self.to_tbl,
|
||||||
|
to_tbl: self.from_tbl,
|
||||||
|
from_col: self.to_col,
|
||||||
|
to_col: self.from_col,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<E, R> RelationBuilder<E, R>
|
impl<E, R> RelationBuilder<E, R>
|
||||||
where
|
where
|
||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
|
@ -25,7 +25,6 @@ pub enum PrimaryKey {
|
|||||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
Fruit,
|
Fruit,
|
||||||
CakeFilling,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ColumnTrait for Column {
|
impl ColumnTrait for Column {
|
||||||
@ -46,10 +45,6 @@ impl RelationTrait for Relation {
|
|||||||
.from(Column::Id)
|
.from(Column::Id)
|
||||||
.to(super::fruit::Column::CakeId)
|
.to(super::fruit::Column::CakeId)
|
||||||
.into(),
|
.into(),
|
||||||
Self::CakeFilling => Entity::has_many(super::cake_filling::Entity)
|
|
||||||
.from(Column::Id)
|
|
||||||
.to(super::cake_filling::Column::CakeId)
|
|
||||||
.into(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,7 +61,7 @@ impl Related<super::filling::Entity> for Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn via() -> Option<RelationDef> {
|
fn via() -> Option<RelationDef> {
|
||||||
Some(Relation::CakeFilling.def())
|
Some(super::cake_filling::Relation::Cake.def().rev())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,4 +69,8 @@ impl Model {
|
|||||||
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
|
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
|
||||||
Entity::find_related().belongs_to::<Entity>(self)
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_filling(&self) -> Select<super::filling::Entity> {
|
||||||
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,11 @@ 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::Cake => Entity::has_many(super::cake::Entity)
|
Self::Cake => Entity::has_one(super::cake::Entity)
|
||||||
.from(Column::CakeId)
|
.from(Column::CakeId)
|
||||||
.to(super::cake::Column::Id)
|
.to(super::cake::Column::Id)
|
||||||
.into(),
|
.into(),
|
||||||
Self::Filling => Entity::has_many(super::filling::Entity)
|
Self::Filling => Entity::has_one(super::filling::Entity)
|
||||||
.from(Column::FillingId)
|
.from(Column::FillingId)
|
||||||
.to(super::filling::Column::Id)
|
.to(super::filling::Column::Id)
|
||||||
.into(),
|
.into(),
|
||||||
|
@ -41,3 +41,19 @@ impl RelationTrait for Relation {
|
|||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Related<super::cake::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
super::cake_filling::Relation::Cake.def()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn via() -> Option<RelationDef> {
|
||||||
|
Some(super::cake_filling::Relation::Filling.def().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Model {
|
||||||
|
pub fn find_cake(&self) -> Select<super::cake::Entity> {
|
||||||
|
Entity::find_related().belongs_to::<Entity>(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user