SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.56k stars 459 forks source link

In migration use enumeration #2221

Closed fan-tastic-z closed 1 month ago

fan-tastic-z commented 1 month ago

Description

According to the example in the document, using enumeration in migration will prompt an error

https://www.sea-ql.org/SeaORM/docs/migration/writing-migration/#schema-creation-methods

use sea_orm::{EnumIter, Iterable};
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(Post::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Post::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Post::Title).string().not_null())
                    .col(ColumnDef::new(Post::Text).string().not_null())
                    .col(
                        ColumnDef::new(Post::Category)
                            .enumeration(Alias::new("category"), Category::iter()),
                    )
                    .to_owned(),
            )
            .await
    }

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

#[derive(DeriveIden)]
enum Post {
    Table,
    Id,
    Title,
    Text,
    Category,
}

#[derive(Iden, EnumIter)]
pub enum Category {
    #[iden = "Feed"]
    Feed,
    #[iden = "Story"]
    Story,
}

Execute sea-orm-cli migrate up

Applying all pending migrations
Applying migration 'm20220101_000001_create_table'
Execution Error: error returned from database: type "category" does not exist
Fail to run migration

sea-orm-cli version: sea-orm-cli 0.12.10 sea-orm-migration: 0.12.0 database: postgres