From 03a926884807122d4598e69e9e587a17d927bef4 Mon Sep 17 00:00:00 2001 From: EETagent Date: Sun, 30 Oct 2022 13:25:18 +0100 Subject: [PATCH 1/2] feat: candidate & parent relations --- entity/src/candidate.rs | 8 ++++++++ entity/src/parent.rs | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/entity/src/candidate.rs b/entity/src/candidate.rs index 221a80e..f020e71 100644 --- a/entity/src/candidate.rs +++ b/entity/src/candidate.rs @@ -35,10 +35,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 {} From 23bae539112f08a36200eb55a4a00c512a15c70d Mon Sep 17 00:00:00 2001 From: EETagent Date: Sun, 30 Oct 2022 13:39:08 +0100 Subject: [PATCH 2/2] feat: candidate fk in parent migration --- migration/src/lib.rs | 3 ++- .../src/m20221024_124701_create_parent.rs | 2 +- ...21030_133428_parent_create_candidate_fk.rs | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 migration/src/m20221030_133428_parent_create_candidate_fk.rs 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