Fix create table for the new relationship between Cake and Lineitem

This commit is contained in:
Sam Samai 2021-07-04 10:40:10 +10:00
parent c5dec7e9dc
commit 23e0b3c590
2 changed files with 17 additions and 9 deletions

View File

@ -62,12 +62,12 @@ impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Order => Entity::belongs_to(super::order::Entity)
.from(Column::Id)
.to(super::order::Column::CustomerId)
.from(Column::OrderId)
.to(super::order::Column::Id)
.into(),
Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::Id)
.to(super::cake::Column::LineitemId)
.from(Column::CakeId)
.to(super::cake::Column::Id)
.into(),
}
}

View File

@ -132,6 +132,11 @@ pub async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.integer()
.not_null(),
)
.col(
ColumnDef::new(lineitem::Column::CakeId)
.integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("FK_lineitem_order")
@ -140,6 +145,14 @@ pub async fn create_lineitem_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("FK_lineitem_cake")
.from(lineitem::Entity, lineitem::Column::CakeId)
.to(cake::Entity, cake::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned();
create_table(db, &stmt).await
@ -183,11 +196,6 @@ pub async fn create_cake_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(cake::Column::Name).string())
.col(ColumnDef::new(cake::Column::Price).float())
.col(ColumnDef::new(cake::Column::BakeryId).integer().not_null())
.col(
ColumnDef::new(cake::Column::LineitemId)
.integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("FK_cake_bakery")