SeaQL/sea-orm · error

Failed to get small integer array

Error message

Failed to get small integer array

What it means

Panic while decoding a Postgres `"char"[]` array column in ProxyRow conversion: `row.try_get::<Option<Vec<i8>>, _>(ordinal).expect("Failed to get small integer array")`. A NULL array is tolerated by the `Option` target, so failure means an element could not be decoded as `i8` — typically NULL elements inside the array (needing `Vec<Option<i8>>`) or the runtime array not actually holding `"char"` values despite the reported type name.

Source

Thrown at sea-orm-sync/src/driver/sqlx_postgres.rs:449

                                .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"),
                        ),
                        #[cfg(feature = "postgres-array")]
                        "SMALLINT[]" | "SMALLSERIAL[]" | "INT2[]" => Value::Array(
                            sea_query::ArrayType::SmallInt,
                            row.try_get::<Option<Vec<i16>>, _>(c.ordinal())
                                .expect("Failed to get small integer array")

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Decode elements as Option: `row.try_get::<Option<Vec<Option<i8>>>, _>(c.ordinal())` and map None elements appropriately.
  2. Remove NULL elements in SQL: `array_remove(col, NULL::"char")` or wrap aggregation with `COALESCE(array_agg(col), ARRAY[]::"char"[])`.
  3. Cast in the query to a plain `"char"[]` if a domain/array-of-domain is in play.
  4. Verify element type via `pg_typeof` and fix the schema or the query cast.
  5. Constrain the source data so elements are always non-null when the model expects i8.

Example fix

// before (panics on NULL elements)
"\"CHAR\"[]" => Value::Array(ArrayType::TinyInt,
    row.try_get::<Option<Vec<i8>>, _>(c.ordinal()).expect("Failed to get small integer array")...),
// after (SQL-side guard)
// SELECT array_remove(col, NULL::"char") AS col FROM t;
"\"CHAR\"[]" => Value::Array(ArrayType::TinyInt,
    row.try_get::<Option<Vec<i8>>, _>(c.ordinal()).expect("Failed to get small integer array")...),
Defensive patterns

Strategy: try-catch

Validate before calling

// SELECT bool_and(e IS NOT NULL) FROM unnest(col::"char"[]) AS e;
// or in SQL: SELECT array_remove(col, NULL::"char") AS col FROM t;

Type guard

fn as_char_array(row: &ProxyRow, col: &str) -> Option<Vec<i8>> {
    match row.value(col) {
        Some(Value::Array(ArrayType::TinyInt, vals)) => Some(
            vals.iter().filter_map(|v| match v {
                Value::TinyInt(Some(x)) => Some(*x),
                _ => None,
            }).collect(),
        ),
        _ => None,
    }
}

Try / catch

let result = std::panic::catch_unwind(|| proxy_row_get_i8_array(&row, "flags"));
match result {
    Ok(v) => v,
    Err(_) => Vec::new(), // or re-query with array_remove(col, NULL::"char")
}

Prevention

When it happens

Trigger: Selecting a `"char"[]` column that contains NULL elements (e.g. `ARRAY['a'::"char", NULL]`); array-of-DOMAIN over `"char"`; or a view/custom type reporting "CHAR[]" whose data is not a smallint-decodable array.

Common situations: Aggregating catalog `"char"` columns with ARRAY_AGG over rows that include NULLs; migrating queries against system catalogs where `"char"` flags are nullable; custom aggregate expressions producing sparse arrays.

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/877e584a40a0c338. Report an issue: GitHub.