SeaQL/sea-orm · error

Failed to get boolean array

Error message

Failed to get boolean array

What it means

Panic raised while decoding a Postgres boolean-array column: the driver matched type name "BOOL[]" and called `row.try_get::<Option<Vec<bool>>, _>(ordinal).expect("Failed to get boolean array")`. The `Option<Vec<bool>>` target already tolerates a NULL array, so the failure means an individual element decode failed (array containing NULLs decodes as `Vec<Option<bool>>` only), or the runtime value is not actually a bool array despite the reported type name.

Source

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

    // https://docs.rs/sqlx-postgres/0.7.2/sqlx_postgres/types/index.html
    use sea_query::Value;
    use sqlx::{Column, Row, TypeInfo};
    crate::ProxyRow {
        values: row
            .columns()
            .iter()
            .map(|c| {
                (
                    c.name().to_string(),
                    match c.type_info().name() {
                        "BOOL" => {
                            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")

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Decode elements as Option: `row.try_get::<Option<Vec<Option<bool>>>, _>(c.ordinal())` and map `None` elements to `Value::TinyInt(None)`/skip, avoiding the element-level decode failure.
  2. Eliminate NULL elements in SQL: `ARRAY_REMOVE(ARRAY_AGG(col), NULL)` or `COALESCE(col, ARRAY[]::boolean[])`.
  3. Cast in the query to a plain `boolean[]` (`col::boolean[]`) if the column is a domain/array-of-domain.
  4. Verify the physical element type with `SELECT pg_typeof(col)`; fix schema or query if it is not boolean.
  5. Set NOT NULL / DEFAULT on elements where the model guarantees non-null booleans.

Example fix

// before (panics when array contains NULL elements)
"BOOL[]" => Value::Array(ArrayType::Bool,
    row.try_get::<Option<Vec<bool>>, _>(c.ordinal()).expect("Failed to get boolean array")...),
// after (SQL-side guard)
// SELECT array_remove(col, NULL) AS col FROM t;
"BOOL[]" => Value::Array(ArrayType::Bool,
    row.try_get::<Option<Vec<bool>>, _>(c.ordinal()).expect("Failed to get boolean array")...),
Defensive patterns

Strategy: try-catch

Validate before calling

// SELECT array_position(col, NULL) IS NOT NULL AS has_nulls FROM t;
// or pre-check: SELECT bool_and(col IS NOT NULL) FROM unnest($1::boolean[]) AS col;

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Selecting a `boolean[]` column that contains NULL elements (e.g. `ARRAY[true, NULL]::boolean[]`) through ProxyRow; or a column whose type name string is "BOOL[]" but whose underlying data is not decodable as `Vec<bool>` (domain/array-of-domain, or a custom type named BOOL[]).

Common situations: Arrays built from nullable expressions (`ARRAY(SELECT b FROM ...)` producing NULL elements); array-of-DOMAIN types over boolean; stale schema where the column was migrated to a different element type; feeding a text representation like '{t,f}' stored in a non-array column that a view reports as BOOL[].

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/228ffe8cd9643731. Report an issue: GitHub.