use sea_orm_migration::{prelude::*, schema::*}; use crate::{m20260503_093223_add_tag_table::Tag, m20260503_125344_add_category_table::Category}; #[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(Todo::Table) .if_not_exists() .col(pk_uuid(Todo::Id)) .col(string(Todo::OwnerId)) .col(string(Todo::Title)) .col(string(Todo::Note)) .col(uuid_null(Todo::CategoryId)) .foreign_key( ForeignKey::create() .from(Todo::Table, Todo::CategoryId) .to(Category::Table, Category::Id) .on_delete(ForeignKeyAction::SetNull) .on_update(ForeignKeyAction::Cascade), ) .to_owned(), ) .await?; manager .create_table( Table::create() .table(TodoTag::Table) .if_not_exists() .primary_key(Index::create().col(TodoTag::TagId).col(TodoTag::TodoId)) .col(uuid(TodoTag::TagId)) .col(uuid(TodoTag::TodoId)) .foreign_key( ForeignKey::create() .from(TodoTag::Table, TodoTag::TagId) .to(Tag::Table, Tag::Id) .on_delete(ForeignKeyAction::Cascade) .on_update(ForeignKeyAction::Cascade), ) .foreign_key( ForeignKey::create() .from(TodoTag::Table, TodoTag::TodoId) .to(Todo::Table, Todo::Id) .on_delete(ForeignKeyAction::Cascade) .on_update(ForeignKeyAction::Cascade), ) .to_owned(), ) .await } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table(Table::drop().table(TodoTag::Table).to_owned()) .await?; manager .drop_table(Table::drop().table(Todo::Table).to_owned()) .await } } #[derive(DeriveIden)] pub enum Todo { Table, Id, OwnerId, Title, Note, CategoryId, } #[derive(DeriveIden)] enum TodoTag { Table, TodoId, TagId, }