diff --git a/migration/src/lib.rs b/migration/src/lib.rs index ce93d1c..307b297 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_create_user; mod m20230322_103045_create_session; +mod m20230322_110525_create_post; pub struct Migrator; @@ -11,6 +12,7 @@ impl MigratorTrait for Migrator { vec![ Box::new(m20220101_000001_create_user::Migration), Box::new(m20230322_103045_create_session::Migration), + Box::new(m20230322_110525_create_post::Migration), ] } } diff --git a/migration/src/m20230322_110525_create_post.rs b/migration/src/m20230322_110525_create_post.rs new file mode 100644 index 0000000..6a91c91 --- /dev/null +++ b/migration/src/m20230322_110525_create_post.rs @@ -0,0 +1,58 @@ +use sea_orm_migration::prelude::*; +use crate::m20220101_000001_create_user::User; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Post::Table) + .if_not_exists() + .col( + ColumnDef::new(Post::Id) + .text() + .not_null() + .primary_key(), + ) + .col(ColumnDef::new(Post::Description).text().not_null()) + .col(ColumnDef::new(Post::Uploader).text().not_null()) + .col(ColumnDef::new(Post::Approver).text().not_null()) + .col(ColumnDef::new(Post::Rating).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("fk-post_uploader-user_id") + .from(Post::Table, Post::Uploader) + .to(User::Table, User::Id) + ) + .foreign_key( + ForeignKey::create() + .name("fk-post_approver-user_id") + .from(Post::Table, Post::Approver) + .to(User::Table, User::Id) + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Post::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Post { + Table, + Id, + Description, + Uploader, + Approver, + Rating +} diff --git a/src/entities/mod.rs b/src/entities/mod.rs index bd72279..c3159df 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.rs @@ -2,5 +2,6 @@ pub mod prelude; +pub mod post; pub mod session; pub mod user; diff --git a/src/entities/post.rs b/src/entities/post.rs new file mode 100644 index 0000000..e6fff62 --- /dev/null +++ b/src/entities/post.rs @@ -0,0 +1,39 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "post")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false, column_type = "Text")] + pub id: String, + #[sea_orm(column_type = "Text")] + pub description: String, + #[sea_orm(column_type = "Text")] + pub uploader: String, + #[sea_orm(column_type = "Text")] + pub approver: String, + pub rating: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::Approver", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User2, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::Uploader", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User1, +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entities/prelude.rs b/src/entities/prelude.rs index c9f9f82..7fe0f45 100644 --- a/src/entities/prelude.rs +++ b/src/entities/prelude.rs @@ -1,4 +1,5 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.1 +pub use super::post::Entity as Post; pub use super::session::Entity as Session; pub use super::user::Entity as User;