SeaQL/sea-orm · error

Failed to get small integer

Error message

Failed to get small integer

What it means

SeaORM's ProxyRow decodes Postgres columns whose type is "CHAR" into Value::TinyInt by calling row.try_get::<i8>(). The .expect() panics when sqlx cannot decode the column's actual value as an i8 — typically because the runtime column type differs from the declared type string (e.g. the column is actually CHAR(n) with multi-byte or longer content, or NULL vs non-NULL expectation mismatch). This is a hard panic, not a recoverable Result.

Source

Thrown at src/driver/sqlx_postgres.rs:456

                            Value::Bool(row.try_get(c.ordinal()).expect("Failed to get boolean"))
                        }
                        #[cfg(feature = "postgres-array")]
                        "BOOL[]" => Value::Array(
                            sea_query::ArrayType::Bool,
                            row.try_get::<Option<Vec<bool>>, _>(c.ordinal())
                                .expect("Failed to get boolean array")
                                .map(|vals| {
                                    Box::new(
                                        vals.into_iter()
                                            .map(|val| Value::Bool(Some(val)))
                                            .collect(),
                                    )
                                }),
                        ),

                        "\"CHAR\"" => Value::TinyInt(
                            row.try_get(c.ordinal())
                                .expect("Failed to get small integer"),
                        ),
                        #[cfg(feature = "postgres-array")]
                        "\"CHAR\"[]" => Value::Array(
                            sea_query::ArrayType::TinyInt,
                            row.try_get::<Option<Vec<i8>>, _>(c.ordinal())
                                .expect("Failed to get small integer array")
                                .map(|vals: Vec<i8>| {
                                    Box::new(
                                        vals.into_iter()
                                            .map(|val| Value::TinyInt(Some(val)))
                                            .collect(),
                                    )
                                }),
                        ),

                        "SMALLINT" | "SMALLSERIAL" | "INT2" => Value::SmallInt(
                            row.try_get(c.ordinal())
                                .expect("Failed to get small integer"),

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Change the column to varchar/text or a fixed smallint type so the reported type matches the i8 decode path
  2. Alter the query to CAST the column (e.g. CAST(col AS int2)) so sqlx decodes it as the expected type
  3. Update your entity model column_type so the type-string branch taken in ProxyRow matches the real schema
  4. Check the column for NULL values and alter to NOT NULL or COALESCE in the query

Example fix

// before
// column: `flag CHAR(3)`

// after
ALTER TABLE my_table ALTER COLUMN flag TYPE varchar(3);
// or cast in the query
SELECT CAST(flag AS int2) AS flag FROM my_table;
Defensive patterns

Strategy: validation

Validate before calling

// Verify column type before querying
let rows = sqlx::query(
    "SELECT data_type, udt_name FROM information_schema.columns WHERE table_name=$1 AND column_name=$2"
).bind("my_table").bind("flag").fetch_all(&db).await?;
assert_eq!(rows[0].get::<String,_>("udt_name"), "char");

Type guard

fn is_char_col(udt: &str) -> bool { udt == "char" || udt == "CHAR" }

Try / catch

// .expect() panics cannot be caught; avoid the branch instead.
// Prefer raw sqlx with Result handling:
let v: Option<i8> = sqlx::query_scalar("SELECT flag FROM my_table").fetch_one(&db).await?;

Prevention

When it happens

Trigger: Calling Entity::find()/raw queries through the proxy driver on Postgres where a column's reported type is "CHAR" but the underlying value cannot be decoded as i8 — e.g. a char(n>1) column, a CHAR column holding non-ASCII data, or a NULL returned where the try_get type does not accept it.

Common situations: Schema drift after migrations changed column types; using char(n) instead of varchar in Postgres; querying through sea-query-binder's ProxyRow with mismatched type maps between what the migration created and what the code expects.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10). Data as JSON: /api/errors/826dc2e7142d05bd. Report an issue: GitHub.