SeaQL/sea-orm · error

Failed to get bytes array

Error message

Failed to get bytes array

What it means

This panic comes from `.expect("Failed to get bytes array")` in `ProxyRow`, decoding a Postgres BYTEA[] column as `Option<Vec<Vec<u8>>>` (behind `postgres-array`). It fires when sqlx cannot decode the array into `Vec<Vec<u8>>` because the array element type OID is not `bytea` (e.g. the column is text[] or was ALTERed), or the value's structure is not a valid bytea array. The `expect` escalates the decode failure into a panic.

Source

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

                                .expect("Failed to get string array")
                                .map(|vals| {
                                    Box::new(
                                        vals.into_iter()
                                            .map(|val| Value::String(Some(val)))
                                            .collect(),
                                    )
                                }),
                        ),

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

                        #[cfg(feature = "with-bigdecimal")]
                        "NUMERIC" => Value::BigDecimal(
                            row.try_get::<Option<bigdecimal::BigDecimal>, _>(c.ordinal())
                                .expect("Failed to get numeric"),
                        ),
                        #[cfg(all(
                            feature = "with-rust_decimal",
                            not(feature = "with-bigdecimal")
                        ))]

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Verify the actual column type (`\d table`); make the entity field match (Vec<Vec<u8>> only for bytea[]).
  2. Cast in the query: `SELECT col::BYTEA[] AS col` to force the element type.
  3. If the data was moved to text[]/jsonb, change the model to String/Json and encode/decode in application code.
  4. Keep sea-orm/sqlx versions aligned with `postgres-array` enabled.

Example fix

// before: entity expects bytea[] but the column was altered to text[]
pub files: Vec<Vec<u8>>,

// after: match the actual type
pub files: Vec<String>,
Defensive patterns

Strategy: validation

Validate before calling

let elem: (String,) = sqlx::query_as(
    "SELECT format_type(a.atttypid, a.atttypmod) FROM pg_attribute a WHERE a.attrelid = $1::regclass AND a.attname = $2"
).bind("my_table").bind("files").fetch_one(&db).await?;
assert_eq!(elem.0, "bytea[]", "expected bytea[]");

Type guard

fn as_bytes_array(v: &sea_orm::Value) -> Option<Vec<Vec<u8>>> {
    match v {
        sea_orm::Value::Array(sea_orm::ArrayType::Bytes, items) => Some(
            items.iter().filter_map(|x| match x {
                sea_orm::Value::Bytes(b) => b.clone(),
                _ => None,
            }).collect(),
        ),
        _ => None,
    }
}

Prevention

When it happens

Trigger: Querying a BYTEA[] column whose real element type is not bytea -- after `ALTER COLUMN ... TYPE TEXT[]`, or when data was imported as string arrays, or a view/cast changes the array type before the driver sees it.

Common situations: Schema drift between entity `Vec<Vec<u8>>` and DB column of another array type. Migrations converting bytea[] to jsonb or text[] for tooling compatibility. FDWs or views remapping array types. sea-orm/sqlx version skew.

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