feat: user_id fk in session table

This commit is contained in:
Sebastian Pravda 2022-10-27 21:05:28 +02:00
parent 162eed786d
commit f440c5ad72
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
6 changed files with 74 additions and 4 deletions

View file

@ -37,6 +37,15 @@ pub struct Model {
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {} pub enum Relation {
#[sea_orm(has_many = "super::session::Entity")]
Session,
}
impl Related<super::session::Entity> for Entity {
fn to() -> RelationDef {
Relation::Session.def()
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View file

@ -14,6 +14,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::candidate::Entity",
from = "Column::UserId",
to = "super::candidate::Column::Application",
on_update = "Cascade",
on_delete = "Cascade"
)]
Candidate,
}
impl Related<super::candidate::Entity> for Entity {
fn to() -> RelationDef {
Relation::Candidate.def()
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View file

@ -5,6 +5,7 @@ mod m20221024_121621_create_candidate;
mod m20221024_124701_create_parent; mod m20221024_124701_create_parent;
mod m20221024_134454_fill_admin; mod m20221024_134454_fill_admin;
mod m20221025_154422_create_session; mod m20221025_154422_create_session;
mod m20221027_194728_session_create_user_fk;
pub struct Migrator; pub struct Migrator;
@ -17,6 +18,7 @@ impl MigratorTrait for Migrator {
Box::new(m20221024_124701_create_parent::Migration), Box::new(m20221024_124701_create_parent::Migration),
Box::new(m20221024_134454_fill_admin::Migration::default()), Box::new(m20221024_134454_fill_admin::Migration::default()),
Box::new(m20221025_154422_create_session::Migration), Box::new(m20221025_154422_create_session::Migration),
Box::new(m20221027_194728_session_create_user_fk::Migration),
] ]
} }
} }

View file

@ -50,7 +50,7 @@ impl MigrationTrait for Migration {
/// Learn more at https://docs.rs/sea-query#iden /// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)] #[derive(Iden)]
enum Candidate { pub enum Candidate {
Table, Table,
Application, Application,
Code, Code,

View file

@ -1,11 +1,29 @@
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
use crate::m20221024_121621_create_candidate::Candidate;
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
pub struct Migration; pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let table = Table::create()
.table(Session::Table)
.if_not_exists()
.col(
ColumnDef::new(Session::Id)
.uuid()
.not_null()
.unique_key()
.primary_key(),
)
.col(ColumnDef::new(Session::HashedToken).string().not_null())
.col(ColumnDef::new(Session::UserId).integer().not_null())
.col(ColumnDef::new(Session::CreatedAt).date_time().not_null())
.col(ColumnDef::new(Session::UpdatedAt).date_time().not_null())
.to_owned();
manager manager
.create_table( .create_table(
Table::create() Table::create()
@ -36,7 +54,7 @@ impl MigrationTrait for Migration {
/// Learn more at https://docs.rs/sea-query#iden /// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)] #[derive(Iden)]
enum Session { pub enum Session {
Table, Table,
Id, Id,
HashedToken, HashedToken,

View file

@ -0,0 +1,26 @@
use sea_orm_migration::prelude::*;
use crate::{m20221025_154422_create_session::Session, m20221024_121621_create_candidate::Candidate};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.create_foreign_key(ForeignKey::create()
.name("user_fk")
.from(Session::Table, Session::UserId)
.to(Candidate::Table, Candidate::Application)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.to_owned()).await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_foreign_key(ForeignKey::drop()
.name("user_fk")
.table(Session::Table)
.to_owned()).await
}
}