diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 502d1b1..4edbbda 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_account; mod m20230520_203638_household; +mod m20230529_184433_ingredients; pub struct Migrator; @@ -11,6 +12,7 @@ impl MigratorTrait for Migrator { vec![ Box::new(m20220101_000001_account::Migration), Box::new(m20230520_203638_household::Migration), + Box::new(m20230529_184433_ingredients::Migration), ] } } diff --git a/migration/src/m20230520_203638_household.rs b/migration/src/m20230520_203638_household.rs index 83e4a88..9df5626 100644 --- a/migration/src/m20230520_203638_household.rs +++ b/migration/src/m20230520_203638_household.rs @@ -6,7 +6,7 @@ use super::m20220101_000001_account::User; pub struct Migration; #[derive(Iden)] -enum Household { +pub enum Household { Table, Name, Id, diff --git a/migration/src/m20230529_184433_ingredients.rs b/migration/src/m20230529_184433_ingredients.rs new file mode 100644 index 0000000..bdac400 --- /dev/null +++ b/migration/src/m20230529_184433_ingredients.rs @@ -0,0 +1,53 @@ +use sea_orm_migration::prelude::*; + +use crate::m20230520_203638_household::Household; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[derive(Iden)] +enum Ingredient { + Table, + Household, + Id, + Name, + Unit, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Ingredient::Table) + .if_not_exists() + .col( + ColumnDef::new(Ingredient::Id) + .not_null() + .auto_increment() + .big_integer() + .primary_key(), + ) + .col(ColumnDef::new(Ingredient::Household).uuid().not_null()) + .col(ColumnDef::new(Ingredient::Name).string().not_null()) + .col(ColumnDef::new(Ingredient::Unit).string()) + .foreign_key( + ForeignKey::create() + .name("FK_ingredients_household") + .from(Ingredient::Table, Ingredient::Household) + .to(Household::Table, Household::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Ingredient::Table).to_owned()) + .await + } +} diff --git a/src/entity/household.rs b/src/entity/household.rs index 4e5864b..1384361 100644 --- a/src/entity/household.rs +++ b/src/entity/household.rs @@ -11,7 +11,16 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::ingredient::Entity")] + Ingredient, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Ingredient.def() + } +} impl Related for Entity { fn to() -> RelationDef { diff --git a/src/entity/ingredient.rs b/src/entity/ingredient.rs new file mode 100644 index 0000000..81ddb71 --- /dev/null +++ b/src/entity/ingredient.rs @@ -0,0 +1,33 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "ingredient")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i64, + pub household: Uuid, + pub name: String, + pub unit: Option, +} + +#[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, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Household.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/mod.rs b/src/entity/mod.rs index dc2707e..aa018ec 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -4,4 +4,5 @@ pub mod prelude; pub mod household; pub mod household_members; +pub mod ingredient; pub mod user; diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 9cbce14..654e5b9 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -2,4 +2,5 @@ pub use super::household::Entity as Household; pub use super::household_members::Entity as HouseholdMembers; +pub use super::ingredient::Entity as Ingredient; pub use super::user::Entity as User;