(toggl-portal): Add project <-> time_entry fk relation

This commit is contained in:
Joshua Coles 2023-11-06 20:13:13 +00:00
parent 64570190c8
commit 3eb3316c31
4 changed files with 67 additions and 1 deletions

View File

@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
mod m20231101_172500_create_time_entry_table;
mod m20231106_134950_create_clients;
mod m20231106_195401_create_projects;
mod m20231106_201029_add_time_entry_project_fk;
pub struct Migrator;
@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
Box::new(m20231101_172500_create_time_entry_table::Migration),
Box::new(m20231106_134950_create_clients::Migration),
Box::new(m20231106_195401_create_projects::Migration),
Box::new(m20231106_201029_add_time_entry_project_fk::Migration),
]
}

View File

@ -0,0 +1,41 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Add foreign key to time entry table referencing project table
manager
.create_foreign_key(
ForeignKey::create()
.name("time_entry_project_id")
.from(TimeEntry::Table, TimeEntry::ProjectId)
.to(Project::Table, Project::TogglId)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Drop foreign key
manager
.drop_foreign_key(ForeignKey::drop().name("time_entry_project_id").to_owned())
.await
}
}
#[derive(DeriveIden)]
enum TimeEntry {
Table,
Id,
ProjectId,
}
#[derive(DeriveIden)]
enum Project {
Table,
Id,
TogglId,
}

View File

@ -28,6 +28,8 @@ pub enum Relation {
on_delete = "NoAction"
)]
Client,
#[sea_orm(has_many = "super::time_entry::Entity")]
TimeEntry,
}
impl Related<super::client::Entity> for Entity {
@ -36,4 +38,10 @@ impl Related<super::client::Entity> for Entity {
}
}
impl Related<super::time_entry::Entity> for Entity {
fn to() -> RelationDef {
Relation::TimeEntry.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -19,6 +19,21 @@ pub struct Model {
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(
belongs_to = "super::project::Entity",
from = "Column::ProjectId",
to = "super::project::Column::TogglId",
on_update = "NoAction",
on_delete = "NoAction"
)]
Project,
}
impl Related<super::project::Entity> for Entity {
fn to() -> RelationDef {
Relation::Project.def()
}
}
impl ActiveModelBehavior for ActiveModel {}