Files
otter/crates/migration/src/m20260526_062833_create_account_table.rs

83 lines
2.7 KiB
Rust

use sea_orm_migration::{prelude::*, schema::*};
#[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("account_category")
.if_not_exists()
.col(pk_auto("id").not_null())
.col(string("name").not_null().unique_key())
.col(string("account_type").not_null())
.col(string("created_at").not_null())
.col(string("updated_at").not_null())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table("account")
.if_not_exists()
.col(pk_auto("id").not_null())
//
.col(string("name").not_null().unique_key())
.col(integer("account_category_id").not_null())
//
.col(string("created_at").not_null())
.col(string("updated_at").not_null())
// Add foreign key constraint to account_category
.foreign_key(
ForeignKey::create()
.name("fk-account-category")
.from("account", "account_category_id")
.to("account_category", "id")
.on_delete(ForeignKeyAction::NoAction)
.on_update(ForeignKeyAction::Restrict),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx-account-account_type")
.table("account_category")
.col("account_type")
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx-account-category_id")
.table("account")
.col("account_category_id")
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table("account").to_owned())
.await?;
manager
.drop_table(Table::drop().table("account_category").to_owned())
.await?;
Ok(())
}
}