Skip to content

Commit

Permalink
feat(migration): update database foreign key constraint
Browse files Browse the repository at this point in the history
default behavior is set to null
  • Loading branch information
adamanteye committed Oct 14, 2024
1 parent aa163a3 commit bb3c427
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sea-orm = { workspace = true, features = [
"sqlx-postgres",
"runtime-tokio-rustls",
"with-uuid",
"macros",
] }
tokio = { workspace = true, features = ["signal", "rt-multi-thread"] }
axum-extra = { workspace = true, features = ["typed-header"] }
Expand Down
24 changes: 22 additions & 2 deletions migration/src/m20241014_000002_create_table_upload.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use sea_orm_migration::prelude::*;

use super::m20241012_000001_create_table_user::User;

pub struct Migration;

impl MigrationName for Migration {
Expand All @@ -11,19 +13,37 @@ impl MigrationName for Migration {
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
let _ = manager
.create_table(
Table::create()
.table(Upload::Table)
.col(ColumnDef::new(Upload::Uuid).uuid().primary_key())
.col(ColumnDef::new(Upload::Typ).string().not_null())
.to_owned(),
)
.await;
manager
.create_foreign_key(
ForeignKey::create()
.from(User::Table, User::Avatar)
.to(Upload::Table, Upload::Uuid)
.on_delete(ForeignKeyAction::SetNull)
.on_update(ForeignKeyAction::SetNull)
.name("FK_USER_AVATAR_UPLOAD")
.to_owned(),
)
.await
}

// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let _ = manager
.drop_foreign_key(
ForeignKey::drop()
.name("FK_USER_AVATAR_UPLOAD")
.table(User::Table)
.to_owned(),
)
.await;
manager
.drop_table(Table::drop().table(Upload::Table).to_owned())
.await
Expand Down
11 changes: 10 additions & 1 deletion src/entity/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ pub struct Model {
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(has_many = "super::user::Entity")]
User,
}

impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
17 changes: 16 additions & 1 deletion src/entity/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ pub struct Model {
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(
belongs_to = "super::upload::Entity",
from = "Column::Avatar",
to = "super::upload::Column::Uuid",
on_update = "SetNull",
on_delete = "SetNull"
)]
Upload,
}

impl Related<super::upload::Entity> for Entity {
fn to() -> RelationDef {
Relation::Upload.def()
}
}

impl ActiveModelBehavior for ActiveModel {}

0 comments on commit bb3c427

Please sign in to comment.