SeaQL / SeaQL/sea-orm

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

Open
#2,605 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:code-gen Area:schema Sqlite
Dominant language
Rust
Stars
9.9k
Forks
735
Avg merge
6h 36m
Merged PRs (30d)
8

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the sea-orm-cli generate entity path for SQLite and inspect how INTEGER UNSIGNED is mapped when producing the model. Reproduce the issue with the records table definition, then verify the generated field uses a signed numerical Rust type and can decode the INTEGER value at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sqlite
Domain
cli, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.