diff --git a/migration/src/m20230618_163416_recipe.rs b/migration/src/m20230618_163416_recipe.rs index 2f68ea7..6948e84 100644 --- a/migration/src/m20230618_163416_recipe.rs +++ b/migration/src/m20230618_163416_recipe.rs @@ -1,6 +1,6 @@ use sea_orm_migration::prelude::*; -use crate::m20230529_184433_ingredients::Ingredient; +use crate::{m20230520_203638_household::Household, m20230529_184433_ingredients::Ingredient}; #[derive(DeriveMigrationName)] pub struct Migration; @@ -9,6 +9,7 @@ pub struct Migration; enum Recipe { Table, Id, + Household, Name, Ranking, } @@ -46,6 +47,15 @@ impl MigrationTrait for Migration { ) .col(ColumnDef::new(Recipe::Name).text().not_null()) .col(ColumnDef::new(Recipe::Ranking).integer().not_null()) + .col(ColumnDef::new(Recipe::Household).uuid().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_recipe_household") + .from(Recipe::Table, Recipe::Household) + .to(Household::Table, Household::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) .to_owned(), ) .await?; diff --git a/src/entity/household.rs b/src/entity/household.rs index 1384361..cc25431 100644 --- a/src/entity/household.rs +++ b/src/entity/household.rs @@ -14,6 +14,8 @@ pub struct Model { pub enum Relation { #[sea_orm(has_many = "super::ingredient::Entity")] Ingredient, + #[sea_orm(has_many = "super::recipe::Entity")] + Recipe, } impl Related for Entity { @@ -22,6 +24,12 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Recipe.def() + } +} + impl Related for Entity { fn to() -> RelationDef { super::household_members::Relation::User.def() diff --git a/src/entity/recipe.rs b/src/entity/recipe.rs index 35182b6..ba0b77e 100644 --- a/src/entity/recipe.rs +++ b/src/entity/recipe.rs @@ -10,14 +10,29 @@ pub struct Model { #[sea_orm(column_type = "Text")] pub name: String, pub ranking: i32, + pub household: Uuid, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { + #[sea_orm( + belongs_to = "super::household::Entity", + from = "Column::Household", + to = "super::household::Column::Id", + on_update = "Restrict", + on_delete = "Restrict" + )] + Household, #[sea_orm(has_many = "super::recipe_steps::Entity")] RecipeSteps, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Household.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::RecipeSteps.def() diff --git a/src/routes/recipe.rs b/src/routes/recipe.rs index 9fd7a32..f5af673 100644 --- a/src/routes/recipe.rs +++ b/src/routes/recipe.rs @@ -18,6 +18,7 @@ pub(super) async fn create_recipe( let model = recipe::ActiveModel { name: ActiveValue::Set(request.name), ranking: ActiveValue::Set(request.rating as i32), + household: ActiveValue::Set(household.id), ..Default::default() };