refactor: rename parent candidate fk to candidate_id

This commit is contained in:
Sebastian Pravda 2023-01-15 16:11:28 +01:00
parent a925aa3665
commit fd08bef4db
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
6 changed files with 18 additions and 18 deletions

View file

@ -6,7 +6,7 @@ use sea_orm::*;
impl Mutation { impl Mutation {
pub async fn create_parent(db: &DbConn, application_id: i32) -> Result<Model, DbErr> { pub async fn create_parent(db: &DbConn, application_id: i32) -> Result<Model, DbErr> {
parent::ActiveModel { parent::ActiveModel {
application: Set(application_id), candidate_id: Set(application_id),
created_at: Set(chrono::offset::Local::now().naive_local()), created_at: Set(chrono::offset::Local::now().naive_local()),
updated_at: Set(chrono::offset::Local::now().naive_local()), updated_at: Set(chrono::offset::Local::now().naive_local()),
..Default::default() ..Default::default()

View file

@ -42,10 +42,10 @@ mod tests {
async fn test_find_parent_by_id() { async fn test_find_parent_by_id() {
let db = get_memory_sqlite_connection().await; let db = get_memory_sqlite_connection().await;
const APPLICATION_ID: i32 = 103158; const CANDIDATE_ID: i32 = 103158;
candidate::ActiveModel { candidate::ActiveModel {
id: Set(APPLICATION_ID), id: Set(CANDIDATE_ID),
personal_identification_number: Set("test".to_string()), personal_identification_number: Set("test".to_string()),
created_at: Set(chrono::offset::Local::now().naive_local()), created_at: Set(chrono::offset::Local::now().naive_local()),
updated_at: Set(chrono::offset::Local::now().naive_local()), updated_at: Set(chrono::offset::Local::now().naive_local()),
@ -55,7 +55,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let parent = parent::ActiveModel { let parent = parent::ActiveModel {
application: Set(APPLICATION_ID), candidate_id: Set(CANDIDATE_ID),
created_at: Set(chrono::offset::Local::now().naive_local()), created_at: Set(chrono::offset::Local::now().naive_local()),
updated_at: Set(chrono::offset::Local::now().naive_local()), updated_at: Set(chrono::offset::Local::now().naive_local()),
..Default::default() ..Default::default()
@ -64,7 +64,7 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let parent = Query::find_candidate_by_id(&db, parent.application) let parent = Query::find_candidate_by_id(&db, parent.candidate_id)
.await .await
.unwrap(); .unwrap();
assert!(parent.is_some()); assert!(parent.is_some());

View file

@ -27,16 +27,10 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { pub enum Relation {
#[sea_orm(has_many = "super::application::Entity")]
Application,
#[sea_orm(has_many = "super::parent::Entity")] #[sea_orm(has_many = "super::parent::Entity")]
Parent, Parent,
} #[sea_orm(has_many = "super::application::Entity")]
Application,
impl Related<super::application::Entity> for Entity {
fn to() -> RelationDef {
Relation::Application.def()
}
} }
impl Related<super::parent::Entity> for Entity { impl Related<super::parent::Entity> for Entity {
@ -45,4 +39,10 @@ impl Related<super::parent::Entity> for Entity {
} }
} }
impl Related<super::application::Entity> for Entity {
fn to() -> RelationDef {
Relation::Application.def()
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View file

@ -7,7 +7,7 @@ use sea_orm::entity::prelude::*;
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
pub application: i32, pub candidate_id: i32,
pub name: Option<String>, pub name: Option<String>,
pub surname: Option<String>, pub surname: Option<String>,
pub telephone: Option<String>, pub telephone: Option<String>,
@ -20,7 +20,7 @@ pub struct Model {
pub enum Relation { pub enum Relation {
#[sea_orm( #[sea_orm(
belongs_to = "super::candidate::Entity", belongs_to = "super::candidate::Entity",
from = "Column::Application", from = "Column::CandidateId",
to = "super::candidate::Column::Id", to = "super::candidate::Column::Id",
on_update = "Cascade", on_update = "Cascade",
on_delete = "Cascade" on_delete = "Cascade"

View file

@ -19,7 +19,7 @@ impl MigrationTrait for Migration {
.primary_key(), .primary_key(),
) )
.col( .col(
ColumnDef::new(Parent::Application) ColumnDef::new(Parent::CandidateId)
.integer() .integer()
.not_null() .not_null()
) )
@ -45,7 +45,7 @@ impl MigrationTrait for Migration {
pub enum Parent { pub enum Parent {
Id, Id,
Table, Table,
Application, CandidateId,
Name, Name,
Surname, Surname,
Telephone, Telephone,

View file

@ -10,7 +10,7 @@ impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.create_foreign_key(ForeignKey::create() manager.create_foreign_key(ForeignKey::create()
.name("candidate_fk") .name("candidate_fk")
.from(Parent::Table, Parent::Application) .from(Parent::Table, Parent::CandidateId)
.to(Candidate::Table, Candidate::Id) .to(Candidate::Table, Candidate::Id)
.on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade) .on_update(ForeignKeyAction::Cascade)