Skip to content

sea-orm-cli generate entity migration select_as, save_as support #1643

Closed
@GoodLucky777

Description

@GoodLucky777

Motivation

I was trying to make Postgresql citext column with migration and generate entity with sea-orm-cli generate entity.

Migration up:

manager
    .get_connection()
    .execute_unprepared("CREATE EXTENSION IF NOT EXISTS citext")
    .await?;

manager
    .create_table(
        Table::create()
            .table(User::Table)
            .if_not_exists()
            .col(ColumnDef::new(User::Id).uuid().not_null().primary_key())
             .col(
                 ColumnDef::new(User::Email)
                    .custom(Alias::new("citext"))
                    .not_null()
                    .unique_key(),
              )
              .to_owned(),
          )
          .await?;

sea-orm-cli generate entity -o src/entity generates:

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: Uuid,
    #[sea_orm(column_type = "custom(\"citext\")", unique)]
    pub email: String,
}

When I try to select the user, this error occurs:

DatabaseError occurred: Query Error: error occurred while decoding column "email": mismatched types; Rust type `core::option::Option<alloc::string::String>` (as SQL type `TEXT`) is not compatible with SQL type `citext`

If I edit the entity to this the error doesn't occurs:

#[sea_orm(column_type = "custom(\"citext\")", select_as = "text", save_as = "citext", unique)]
pub email: String

Proposed Solutions

Able to set select_as, save_as in migration.

Current Workarounds

Edit the entity manually when I generate it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions