Skip to content

Uuid/Datetime/etc. types (instead of String) for generated models with sqlite databases #924

@Dash-L

Description

@Dash-L

Update (not really since I never submitted the original but oh well)

This was originally a bug report, but now it is really a feature request, but I'm keeping the bug report template because I already wrote it out and I suspect others might come here thinking this is a bug.

After writing out this issue, I asked on the discord just in case, and found out that there has been some discussion of this in the past, not just for uuids but for datetime and other types as well, because sqlite doesn't support those types and they will be TEXT in the database anyway, but I still think it would be nice for type-safety to have the models be the correct types in rust anyway (though from my brief skimming of the discussion on discord, it seems like there are some drawbacks to this, at least in development of the ORM?). I'd like to continue the discussion of this here, or if it has already been started somewhere else if someone could point me to it that would be great.

Original discussion on discord for anyone interested: https://discord.com/channels/873880840487206962/900758376164757555/997017818933497967

Also I'm not really sure what to title this issue, so the title right now is kind of confusing, sorry!

Original Issue:

Description

I want to use uuids for my ids in my models, so in the migration I have

.col(ColumnDef::new(User::Id).uuid().not_null().primary_key())

but when I generate with sea-orm-cli generate entity, the generated model looks like

pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: String,
    // ...
}

which I suppose technically works because I can turn Uuids into Strings, but that's not particularly type-safe.
Also I can just change the type of the id field manually, and it all seems to work, so my guess is this is not intended behavior/I am missing something, or if it is intended behavior it should probably be changed.

Steps to Reproduce

  1. Create a table with a field using .uuid() in a migration
  2. Generate the entities with sea-orm-cli generate entity
  3. Get a struct in which the type of the field is String, not Uuid

Expected Behavior

The field is type Uuid

Actual Behavior

The field is type String

Reproduces How Often

Every time

Versions

OS: Void Linux
Database: sqlite3

$ cargo tree | grep sea-
    ├── sea-orm v0.9.1
    │   ├── sea-orm-macros v0.9.1 (proc-macro)
    │   ├── sea-query v0.26.2
    │   │   ├── sea-query-derive v0.2.0 (proc-macro)
    │   │   ├── sea-query-driver v0.2.0 (proc-macro)
    │   ├── sea-strum v0.23.0
    │   │   └── sea-strum_macros v0.23.0 (proc-macro)
└── sea-orm-migration v0.9.1
    ├── sea-orm v0.9.1 (*)
    ├── sea-orm-cli v0.9.1
    │   ├── sea-schema v0.9.3
    │   │   ├── sea-query v0.26.2 (*)
    │   │   └── sea-schema-derive v0.1.0 (proc-macro)
    ├── sea-schema v0.9.3 (*)
├── sea-orm v0.9.1 (*)

Additional Information

// migration/src/m20220728_000001_create_user_table.rs

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(User::Table)
                    .col(ColumnDef::new(User::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(User::Name).string().not_null())
                    .to_owned(),
            )
            .await
    }

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

/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum User {
    Table,
    Id,
    Name,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions