diff --git a/entity/src/candidate.rs b/entity/src/candidate.rs index 4861e7c..fe2f725 100644 --- a/entity/src/candidate.rs +++ b/entity/src/candidate.rs @@ -28,10 +28,18 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { + #[sea_orm(has_one = "super::parent::Entity")] + Parent, #[sea_orm(has_many = "super::session::Entity")] Session, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Parent.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::Session.def() diff --git a/entity/src/parent.rs b/entity/src/parent.rs index 7e1b00c..4d107af 100644 --- a/entity/src/parent.rs +++ b/entity/src/parent.rs @@ -14,6 +14,19 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm( + belongs_to = "super::candidate::Entity", + from = "Column::Application", + to = "super::candidate::Column::Application" + )] + Candidate, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Candidate.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index a8f21b1..d8329d8 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -6,7 +6,7 @@ mod m20221024_124701_create_parent; mod m20221024_134454_fill_admin; mod m20221025_154422_create_session; mod m20221027_194728_session_create_user_fk; - +mod m20221030_133428_parent_create_candidate_fk; pub struct Migrator; #[async_trait::async_trait] @@ -19,6 +19,7 @@ impl MigratorTrait for Migrator { Box::new(m20221024_134454_fill_admin::Migration::default()), Box::new(m20221025_154422_create_session::Migration), Box::new(m20221027_194728_session_create_user_fk::Migration), + Box::new(m20221030_133428_parent_create_candidate_fk::Migration), ] } } diff --git a/migration/src/m20221024_124701_create_parent.rs b/migration/src/m20221024_124701_create_parent.rs index b53148e..99a0b14 100644 --- a/migration/src/m20221024_124701_create_parent.rs +++ b/migration/src/m20221024_124701_create_parent.rs @@ -37,7 +37,7 @@ impl MigrationTrait for Migration { } #[derive(Iden)] -enum Parent { +pub enum Parent { Table, Application, Name, diff --git a/migration/src/m20221030_133428_parent_create_candidate_fk.rs b/migration/src/m20221030_133428_parent_create_candidate_fk.rs new file mode 100644 index 0000000..1459bc5 --- /dev/null +++ b/migration/src/m20221030_133428_parent_create_candidate_fk.rs @@ -0,0 +1,26 @@ +use sea_orm_migration::prelude::*; + +use crate::{m20221024_124701_create_parent::Parent, m20221024_121621_create_candidate::Candidate}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.create_foreign_key(ForeignKey::create() + .name("candidate_fk") + .from(Parent::Table, Candidate::Application) + .to(Parent::Table, Parent::Application) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned()).await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.drop_foreign_key(ForeignKey::drop() + .name("candidate_fk") + .table(Parent::Table) + .to_owned()).await + } +} \ No newline at end of file