Merge pull request #24 from EETagent/candidate_parent_relations

Candidate / Parent relations
This commit is contained in:
Vojtěch Jungmann 2022-10-30 13:51:27 +01:00 committed by GitHub
commit 6c0e187c5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 3 deletions

View file

@ -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<super::parent::Entity> for Entity {
fn to() -> RelationDef {
Relation::Parent.def()
}
}
impl Related<super::session::Entity> for Entity {
fn to() -> RelationDef {
Relation::Session.def()

View file

@ -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<super::candidate::Entity> for Entity {
fn to() -> RelationDef {
Relation::Candidate.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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),
]
}
}

View file

@ -37,7 +37,7 @@ impl MigrationTrait for Migration {
}
#[derive(Iden)]
enum Parent {
pub enum Parent {
Table,
Application,
Name,

View file

@ -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
}
}