SeaQL / sea-orm

šŸš An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
7.37k stars 517 forks source link

Incorrect type in entity generation #2335

Open chungwong opened 2 months ago

chungwong commented 2 months ago

Description

character varying(128) in incorrectly generated as 128u32

Steps to Reproduce

Consider the following table

                      Table "public.sessions"
 Column  |          Type          | Collation | Nullable | Default 
---------+------------------------+-----------+----------+---------
 id      | character varying(128) |           | not null | 
 expires | integer                |           |          | 
 session | text                   |           | not null | 
Indexes:
    "sessions_pkey" PRIMARY KEY, btree (id)

id is a session id with length 128 and I believe it is a UUID and hence the 128 length. With sea-orm-cli generate entity, the type is incorrectly produced.

     type EntityName = Entity;
     fn def(&self) -> ColumnDef {
         match self {
-            Self::Id => ColumnType::Text.def(),
+            Self::Id => ColumnType::String(Some(128u32)).def(),
             Self::Expires => ColumnType::Integer.def().null(),
             Self::Session => ColumnType::Text.def(),
         }

If I don't manually coerce it to ColumnType::Text.def(), it won't compile

   |
59 |             Self::Id => ColumnType::String(Some(128u32)).def(),
   |                         ------------------ ^^^^^^^^^^^^ expected `StringLen`, found `Option<u32>`
   |                         |
   |                         arguments to this enum variant are incorrect
   |
   = note: expected enum `StringLen`
              found enum `std::option::Option<u32>`
note: tuple variant defined here
  --> /home/chung/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sea-query-0.32.0-rc.1/src/table/column.rs:62:5
   |
62 |     String(StringLen),
   |     ^^^^^^

Expected Behavior

After referencing this table, I cannot tell what type should be.

Reproduces How Often

It is always reproducible

Workarounds

Change to ColumnType::Text.def() manually.

Versions

āÆ cargo tree | grep sea-
ā”‚   ā”œā”€ā”€ sea-orm v1.1.0-rc.1
ā”‚   ā”‚   ā”œā”€ā”€ sea-orm-macros v1.1.0-rc.1 (proc-macro)
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ sea-bae v0.2.0 (proc-macro)
ā”‚   ā”‚   ā”œā”€ā”€ sea-query v0.32.0-rc.1

psql (16.1 (Debian 16.1-1.pgdg100+1), server 9.6.24) Debian 12

david-d-h commented 1 month ago

I still have this same issue, anybody looked into this?