Closed5

set up SeaORM(about migration)

Kγ0suKεKγ0suKε

Initialize

cd root
cargo install sea-orm-cli
sea-orm-cli migrate init
sea-orm-cli migrate generate {MIGRATION FILE NAME}
Kγ0suKεKγ0suKε

setting Migration file

use sea_orm_migration::prelude::*;

#[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(Task::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Task::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    // type of string
                    .col(ColumnDef::new(Task::Title).string().not_null())
                   type of boolean
                    .col(ColumnDef::new(Task::IsClosed).boolean().not_null())
                    .col(ColumnDef::new(Task::CreatedAt).date_time().not_null())
                    .col(ColumnDef::new(Task::UpdatedAt).date_time().not_null())

                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Task::Table).to_owned())
            .await
    }
}

/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
// if explicitly define to table name, then to write name at rename area
#[iden(rename = "{here}")]
enum Task {
    Table,
    Id,
    Title,
    IsClosed,
    CreatedAt,
    UpdatedAt
}
Kγ0suKεKγ0suKε

migration

sea-orm-cli migrate up

or

sea-orm-cli migrate down
このスクラップは2024/10/14にクローズされました