sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

cli generate entity -> sqlite INTEGER UNSIGNED defined as String in Model but as integer later

Open dgasparri opened this issue 8 months ago • 0 comments

Description

When creating the entities with the cli "generate entity" for a Sqlite database, the columns with type "INTEGER UNSIGNED" are created as String:

    #[sea_orm(column_type = "custom(\"INTEGER UNSIGNED\")")]
    pub session: String,
    #[sea_orm(column_type = "custom(\"INTEGER UNSIGNED\")")]
    pub entry_date: String,

but then at runtime:

Error: 500 Internal Server Error (db): Query Error: error occurred while decoding column "session": mismatched types; Rust type core::option::Option<alloc::string::String> (as SQL type TEXT) is not compatible with SQL type INTEGER

Steps to Reproduce

In a Sqlite db create a table with a INTEGER UNSIGNED field:

CREATE TABLE "records" (
                "record_id" INTEGER NOT NULL PRIMARY KEY ASC,
                "session" INTEGER UNSIGNED NOT NULL CHECK ("session" >= 0),
                "command" TEXT NOT NULL);

Run "sea-orm-cli generate entity", and the model generated will be with a custom generated custom("INTEGER UNSIGNED") as column type:

#[sea_orm(table_name = "backoffice_records")]
pub struct Model {
    ...
    #[sea_orm(column_type = "custom(\"INTEGER UNSIGNED\")")]
    pub session: String,
    ...
}

When using the model, SeaORM will give: "Query Error: error occurred while decoding column "session": mismatched types; Rust type core::option::Option<alloc::string::String> (as SQL type TEXT) is not compatible with SQL type INTEGER"

Expected Behavior

"UNSIGNED INTEGER" is an allowed type for Sqlite, marked as affine to Integer and casted accordingly. The field in the model should have a signed numerical type (Sqlite AFAIK does not enforce the unsigned requirement, so a Rust unsigned type could introduce a bug)

Source: https://www.sqlite.org/datatype3.html

Actual Behavior

#[sea_orm(column_type = "custom(\"INTEGER UNSIGNED\")")] 
pub session: String,

Workarounds

I'm changing the entities manually

Reproducible Example

Versions

sea-orm-cli 1.1.11 on Windows 11

dgasparri avatar May 23 '25 08:05 dgasparri