(toggl-portal): Add project <-> time_entry fk relation
This commit is contained in:
parent
64570190c8
commit
3eb3316c31
@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
|
|||||||
mod m20231101_172500_create_time_entry_table;
|
mod m20231101_172500_create_time_entry_table;
|
||||||
mod m20231106_134950_create_clients;
|
mod m20231106_134950_create_clients;
|
||||||
mod m20231106_195401_create_projects;
|
mod m20231106_195401_create_projects;
|
||||||
|
mod m20231106_201029_add_time_entry_project_fk;
|
||||||
|
|
||||||
pub struct Migrator;
|
pub struct Migrator;
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
|
|||||||
Box::new(m20231101_172500_create_time_entry_table::Migration),
|
Box::new(m20231101_172500_create_time_entry_table::Migration),
|
||||||
Box::new(m20231106_134950_create_clients::Migration),
|
Box::new(m20231106_134950_create_clients::Migration),
|
||||||
Box::new(m20231106_195401_create_projects::Migration),
|
Box::new(m20231106_195401_create_projects::Migration),
|
||||||
|
Box::new(m20231106_201029_add_time_entry_project_fk::Migration),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
41
migration/src/m20231106_201029_add_time_entry_project_fk.rs
Normal file
41
migration/src/m20231106_201029_add_time_entry_project_fk.rs
Normal 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,
|
||||||
|
}
|
||||||
@ -28,6 +28,8 @@ pub enum Relation {
|
|||||||
on_delete = "NoAction"
|
on_delete = "NoAction"
|
||||||
)]
|
)]
|
||||||
Client,
|
Client,
|
||||||
|
#[sea_orm(has_many = "super::time_entry::Entity")]
|
||||||
|
TimeEntry,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Related<super::client::Entity> for Entity {
|
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 {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|||||||
@ -19,6 +19,21 @@ pub struct Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[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 {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user