Add post table

This commit is contained in:
Evie Viau-Chow-Stuart 2023-03-22 04:36:23 -07:00
parent acccf035b8
commit 9b2449c846
Signed by: evie
GPG key ID: 928652CDFCEC8099
5 changed files with 101 additions and 0 deletions

View file

@ -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),
]
}
}

View file

@ -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
}

View file

@ -2,5 +2,6 @@
pub mod prelude;
pub mod post;
pub mod session;
pub mod user;

39
src/entities/post.rs Normal file
View file

@ -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 {}

View file

@ -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;