From f961c56a7b9af244c1cc732d146893cc04b04c91 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 20 Jul 2024 01:24:01 +0800 Subject: [PATCH] =?UTF-8?q?db=E5=B0=B1=E8=BF=99=E6=A0=B7=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../m20240719_00001_create_main_data_table.rs | 2 +- .../m20240719_00002_create_long_data_table.rs | 7 ++++++- sr_download/src/db/long_data.rs | 17 ++++++++++++++++- sr_download/src/db/main_data.rs | 15 ++++++++++++--- sr_download/src/db/sea_orm_active_enums.rs | 4 ++-- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/migration/src/m20240719_00001_create_main_data_table.rs b/migration/src/m20240719_00001_create_main_data_table.rs index e045f5b..7afc208 100644 --- a/migration/src/m20240719_00001_create_main_data_table.rs +++ b/migration/src/m20240719_00001_create_main_data_table.rs @@ -1,6 +1,6 @@ use sea_orm::{EnumIter, Iterable}; use sea_orm_migration::prelude::extension::postgres::Type; -use sea_orm_migration::{manager, prelude::*}; +use sea_orm_migration::prelude::*; #[derive(DeriveMigrationName)] pub struct Migration; diff --git a/migration/src/m20240719_00002_create_long_data_table.rs b/migration/src/m20240719_00002_create_long_data_table.rs index bdbeff3..18f8614 100644 --- a/migration/src/m20240719_00002_create_long_data_table.rs +++ b/migration/src/m20240719_00002_create_long_data_table.rs @@ -13,7 +13,12 @@ impl MigrationTrait for Migration { Table::create() .table(LongData::Table) .if_not_exists() - .col(ColumnDef::new(LongData::SaveId).integer().not_null().primary_key()) + .col( + ColumnDef::new(LongData::SaveId) + .integer() + .not_null() + .primary_key(), + ) .col(ColumnDef::new(LongData::Len).string().not_null()) .col(ColumnDef::new(LongData::Text).string().not_null()) .foreign_key( diff --git a/sr_download/src/db/long_data.rs b/sr_download/src/db/long_data.rs index 4032600..1956ef5 100644 --- a/sr_download/src/db/long_data.rs +++ b/sr_download/src/db/long_data.rs @@ -12,6 +12,21 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm( + belongs_to = "super::main_data::Entity", + from = "Column::SaveId", + to = "super::main_data::Column::SaveId", + on_update = "Cascade", + on_delete = "Cascade" + )] + MainData, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::MainData.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/sr_download/src/db/main_data.rs b/sr_download/src/db/main_data.rs index 3a0526a..1fbdfa7 100644 --- a/sr_download/src/db/main_data.rs +++ b/sr_download/src/db/main_data.rs @@ -1,6 +1,6 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15 -use super::sea_orm_active_enums::SaveTypeEnum; +use super::sea_orm_active_enums::SaveType; use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] @@ -8,13 +8,22 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub save_id: i32, - pub save_type: SaveTypeEnum, + pub save_type: SaveType, pub blake_hash: String, pub len: i32, pub short_data: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::long_data::Entity")] + LongData, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::LongData.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/sr_download/src/db/sea_orm_active_enums.rs b/sr_download/src/db/sea_orm_active_enums.rs index 2e05887..9e497fe 100644 --- a/sr_download/src/db/sea_orm_active_enums.rs +++ b/sr_download/src/db/sea_orm_active_enums.rs @@ -3,8 +3,8 @@ use sea_orm::entity::prelude::*; #[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)] -#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "save_type_enum")] -pub enum SaveTypeEnum { +#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "save_type")] +pub enum SaveType { #[sea_orm(string_value = "none")] None, #[sea_orm(string_value = "save")]