Relation with optional ForeignKeyAction

This commit is contained in:
Billy Chan 2021-09-01 10:35:51 +08:00
parent 1ad1767457
commit 5073c6f4aa
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
9 changed files with 65 additions and 19 deletions

View File

@ -1,9 +1,9 @@
pub use crate::{ pub use crate::{
error::*, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType, error::*, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType,
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity, DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, ForeignKeyAction, Iden,
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef, IdenStatic, ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related,
RelationTrait, Select, Value, RelationDef, RelationTrait, Select, Value,
}; };
#[cfg(feature = "with-json")] #[cfg(feature = "with-json")]

View File

@ -9,6 +9,8 @@ pub enum RelationType {
HasMany, HasMany,
} }
pub type ForeignKeyAction = sea_query::ForeignKeyAction;
pub trait RelationTrait: Iterable + Debug + 'static { pub trait RelationTrait: Iterable + Debug + 'static {
fn def(&self) -> RelationDef; fn def(&self) -> RelationDef;
} }
@ -35,6 +37,8 @@ pub struct RelationDef {
pub from_col: Identity, pub from_col: Identity,
pub to_col: Identity, pub to_col: Identity,
pub is_owner: bool, pub is_owner: bool,
pub on_delete: Option<ForeignKeyAction>,
pub on_update: Option<ForeignKeyAction>,
} }
pub struct RelationBuilder<E, R> pub struct RelationBuilder<E, R>
@ -49,6 +53,8 @@ where
from_col: Option<Identity>, from_col: Option<Identity>,
to_col: Option<Identity>, to_col: Option<Identity>,
is_owner: bool, is_owner: bool,
on_delete: Option<ForeignKeyAction>,
on_update: Option<ForeignKeyAction>,
} }
impl RelationDef { impl RelationDef {
@ -61,6 +67,8 @@ impl RelationDef {
from_col: self.to_col, from_col: self.to_col,
to_col: self.from_col, to_col: self.from_col,
is_owner: !self.is_owner, is_owner: !self.is_owner,
on_delete: self.on_delete,
on_update: self.on_update,
} }
} }
} }
@ -79,6 +87,8 @@ where
from_col: None, from_col: None,
to_col: None, to_col: None,
is_owner, is_owner,
on_delete: None,
on_update: None,
} }
} }
@ -91,6 +101,8 @@ where
from_col: Some(rel.from_col), from_col: Some(rel.from_col),
to_col: Some(rel.to_col), to_col: Some(rel.to_col),
is_owner, is_owner,
on_delete: None,
on_update: None,
} }
} }
@ -109,6 +121,16 @@ where
self.to_col = Some(identifier.identity_of()); self.to_col = Some(identifier.identity_of());
self self
} }
pub fn on_delete(mut self, action: ForeignKeyAction) -> Self {
self.on_delete = Some(action);
self
}
pub fn on_update(mut self, action: ForeignKeyAction) -> Self {
self.on_update = Some(action);
self
}
} }
impl<E, R> From<RelationBuilder<E, R>> for RelationDef impl<E, R> From<RelationBuilder<E, R>> for RelationDef
@ -124,6 +146,8 @@ where
from_col: b.from_col.unwrap(), from_col: b.from_col.unwrap(),
to_col: b.to_col.unwrap(), to_col: b.to_col.unwrap(),
is_owner: b.is_owner, is_owner: b.is_owner,
on_delete: b.on_delete,
on_update: b.on_update,
} }
} }
} }

View File

@ -89,6 +89,12 @@ where
foreign_key_stmt.to_col(o3); foreign_key_stmt.to_col(o3);
} }
} }
if let Some(action) = relation.on_delete {
foreign_key_stmt.on_delete(action);
}
if let Some(action) = relation.on_update {
foreign_key_stmt.on_update(action);
}
stmt.foreign_key( stmt.foreign_key(
foreign_key_stmt foreign_key_stmt
.name(&format!( .name(&format!(

View File

@ -60,6 +60,8 @@ impl RelationTrait for Relation {
Self::Bakery => Entity::belongs_to(super::bakery::Entity) Self::Bakery => Entity::belongs_to(super::bakery::Entity)
.from(Column::BakeryId) .from(Column::BakeryId)
.to(super::bakery::Column::Id) .to(super::bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
} }
} }

View File

@ -67,6 +67,8 @@ impl RelationTrait for Relation {
Self::Bakery => Entity::belongs_to(super::bakery::Entity) Self::Bakery => Entity::belongs_to(super::bakery::Entity)
.from(Column::BakeryId) .from(Column::BakeryId)
.to(super::bakery::Column::Id) .to(super::bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(), Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(),
} }

View File

@ -56,10 +56,14 @@ impl RelationTrait for Relation {
Self::Cake => Entity::belongs_to(super::cake::Entity) Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId) .from(Column::CakeId)
.to(super::cake::Column::Id) .to(super::cake::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
Self::Baker => Entity::belongs_to(super::baker::Entity) Self::Baker => Entity::belongs_to(super::baker::Entity)
.from(Column::BakerId) .from(Column::BakerId)
.to(super::baker::Column::Id) .to(super::baker::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
} }
} }

View File

@ -64,10 +64,14 @@ impl RelationTrait for Relation {
Self::Order => Entity::belongs_to(super::order::Entity) Self::Order => Entity::belongs_to(super::order::Entity)
.from(Column::OrderId) .from(Column::OrderId)
.to(super::order::Column::Id) .to(super::order::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
Self::Cake => Entity::belongs_to(super::cake::Entity) Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId) .from(Column::CakeId)
.to(super::cake::Column::Id) .to(super::cake::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
} }
} }

View File

@ -65,10 +65,14 @@ impl RelationTrait for Relation {
Self::Bakery => Entity::belongs_to(super::bakery::Entity) Self::Bakery => Entity::belongs_to(super::bakery::Entity)
.from(Column::BakeryId) .from(Column::BakeryId)
.to(super::bakery::Column::Id) .to(super::bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
Self::Customer => Entity::belongs_to(super::customer::Entity) Self::Customer => Entity::belongs_to(super::customer::Entity)
.from(Column::CustomerId) .from(Column::CustomerId)
.to(super::customer::Column::Id) .to(super::customer::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.into(), .into(),
Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(), Self::Lineitem => Entity::has_many(super::lineitem::Entity).into(),
} }

View File

@ -68,8 +68,8 @@ pub async fn create_baker_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.name("fk-baker-bakery") .name("fk-baker-bakery")
.from(baker::Entity, baker::Column::BakeryId) .from(baker::Entity, baker::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id) .to(bakery::Entity, bakery::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.to_owned(); .to_owned();
@ -126,16 +126,16 @@ pub async fn create_order_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.name("fk-order-bakery") .name("fk-order-bakery")
.from(order::Entity, order::Column::BakeryId) .from(order::Entity, order::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id) .to(bakery::Entity, bakery::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.foreign_key( .foreign_key(
ForeignKey::create() ForeignKey::create()
.name("fk-order-customer") .name("fk-order-customer")
.from(order::Entity, order::Column::CustomerId) .from(order::Entity, order::Column::CustomerId)
.to(customer::Entity, customer::Column::Id) .to(customer::Entity, customer::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.to_owned(); .to_owned();
@ -178,16 +178,16 @@ pub async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.name("fk-lineitem-order") .name("fk-lineitem-order")
.from(lineitem::Entity, lineitem::Column::OrderId) .from(lineitem::Entity, lineitem::Column::OrderId)
.to(order::Entity, order::Column::Id) .to(order::Entity, order::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.foreign_key( .foreign_key(
ForeignKey::create() ForeignKey::create()
.name("fk-lineitem-cake") .name("fk-lineitem-cake")
.from(lineitem::Entity, lineitem::Column::CakeId) .from(lineitem::Entity, lineitem::Column::CakeId)
.to(cake::Entity, cake::Column::Id) .to(cake::Entity, cake::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.to_owned(); .to_owned();
@ -219,16 +219,16 @@ pub async fn create_cakes_bakers_table(db: &DbConn) -> Result<ExecResult, DbErr>
.name("fk-cakes_bakers-cake") .name("fk-cakes_bakers-cake")
.from(cakes_bakers::Entity, cakes_bakers::Column::CakeId) .from(cakes_bakers::Entity, cakes_bakers::Column::CakeId)
.to(cake::Entity, cake::Column::Id) .to(cake::Entity, cake::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.foreign_key( .foreign_key(
ForeignKey::create() ForeignKey::create()
.name("fk-cakes_bakers-baker") .name("fk-cakes_bakers-baker")
.from(cakes_bakers::Entity, cakes_bakers::Column::BakerId) .from(cakes_bakers::Entity, cakes_bakers::Column::BakerId)
.to(baker::Entity, baker::Column::Id) .to(baker::Entity, baker::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.to_owned(); .to_owned();
@ -258,8 +258,8 @@ pub async fn create_cake_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.name("fk-cake-bakery") .name("fk-cake-bakery")
.from(cake::Entity, cake::Column::BakeryId) .from(cake::Entity, cake::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id) .to(bakery::Entity, bakery::Column::Id)
// .on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
// .on_update(ForeignKeyAction::Cascade), .on_update(ForeignKeyAction::Cascade),
) )
.col( .col(
ColumnDef::new(cake::Column::GlutenFree) ColumnDef::new(cake::Column::GlutenFree)