From 7ef02d05d015e2d433cdcd3f6883e0a6bf9aff9c Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 17 Jul 2024 19:15:05 +0300 Subject: [PATCH] feat: Use `TableCreateStatement::foreign_key` for foreign keys Signed-off-by: Awiteb --- .../src/create_incoming_chat_requests_table.rs | 13 +++++++++---- .../src/create_outgoing_chat_requests_table.rs | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs b/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs index 7289225..fee1dec 100644 --- a/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs +++ b/crates/oxidetalis_migrations/src/create_incoming_chat_requests_table.rs @@ -21,6 +21,8 @@ use sea_orm_migration::prelude::*; +use crate::create_users_table::Users; + #[derive(DeriveMigrationName)] pub struct Migration; @@ -39,10 +41,13 @@ impl MigrationTrait for Migration { .auto_increment() .primary_key(), ) - .col( - ColumnDef::new(InChatRequests::RecipientId) - .big_integer() - .not_null(), + .foreign_key( + ForeignKey::create() + .name("fk-in_chat_requests-users") + .from(InChatRequests::Table, InChatRequests::RecipientId) + .to(Users::Table, Users::Id) + .on_update(ForeignKeyAction::NoAction) + .on_delete(ForeignKeyAction::Cascade), ) .col(ColumnDef::new(InChatRequests::Sender).string().not_null()) .col( diff --git a/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs b/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs index 7db4b81..6880157 100644 --- a/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs +++ b/crates/oxidetalis_migrations/src/create_outgoing_chat_requests_table.rs @@ -21,6 +21,8 @@ use sea_orm_migration::prelude::*; +use crate::create_users_table::Users; + #[derive(DeriveMigrationName)] pub struct Migration; @@ -39,10 +41,13 @@ impl MigrationTrait for Migration { .auto_increment() .primary_key(), ) - .col( - ColumnDef::new(OutChatRequests::SenderId) - .big_integer() - .not_null(), + .foreign_key( + ForeignKey::create() + .name("fk-out_chat_requests-users") + .from(OutChatRequests::Table, OutChatRequests::SenderId) + .to(Users::Table, Users::Id) + .on_update(ForeignKeyAction::NoAction) + .on_delete(ForeignKeyAction::Cascade), ) .col( ColumnDef::new(OutChatRequests::Recipient)