SeaQL/sea-orm · error

Failed to get bytes

Error message

Failed to get bytes

What it means

This panic is raised by `.expect("Failed to get bytes")` in ProxyRow's PostgreSQL backend (src/driver/sqlx_postgres.rs:576). For BYTEA columns the driver decodes `Option<Vec<u8>>`; it panics when sqlx cannot decode the value as bytea. Typically the value is actually text or another binary variant, or the metadata type name says BYTEA for a non-bytea column.

Solutions

  1. Confirm the column type is genuinely bytea (`SELECT format_type(atttypid, ...)`) and cast in SQL (`col::bytea`) when needed.
  2. Refresh the query/column metadata after any schema migration so ordinals and types match.
  3. Align sea-orm and sqlx versions and rebuild; bytea handling changed across sqlx versions.
  4. Enable RUST_LOG=sqlx=debug to see the real decode error before the expect panics.
  5. Replace expect with an error mapped to DbErr::Custom naming the column.

Example fix

// before
"BYTEA" => Value::Bytes(
    row.try_get::<Option<Vec<u8>>, _>(c.ordinal()).expect("Failed to get bytes"),
)
// after
"BYTEA" => Value::Bytes(
    row.try_get::<Option<Vec<u8>>, _>(c.ordinal())
        .map_err(|e| DbErr::Custom(format!("bytea decode failed col {}: {e}", c.ordinal())))?,
)
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the column is bytea:
// SELECT data_type FROM information_schema.columns
//   WHERE table_name=$1 AND column_name=$2;  -- expect 'bytea'
// Or cast: SELECT col::bytea FROM ...

Type guard

fn is_bytea_col(c: &ProxyColumn) -> bool {
    c.ty == "BYTEA"
}

Try / catch

std::panic::catch_unwind(|| read_row(row))
    .map_err(|p| DbErr::Custom(format!("bytea decode panic: {p:?}")))?;

Prevention

When it happens

Trigger: Reading a bytea column whose rows were written as text (or vice versa) via a different client, selecting a large-object reference or a view column typed differently from bytea, or a stale cached column list where ordinal/type no longer matches the actual result row.

Common situations: Schema drift after ALTER COLUMN TYPE, using a proxy/sharding layer with a cached schema, sqlx version mismatch changing bytea decode behavior, or comparing a `text` hex-encoded blob mislabeled as bytea.

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

Appendix: source

Thrown at src/driver/sqlx_postgres.rs:576

                                .expect("Failed to get string"),
                        ),
                        #[cfg(feature = "postgres-array")]
                        "VARCHAR[]" | "CHAR[]" | "TEXT[]" | "NAME[]" => Value::Array(
                            sea_query::ArrayType::String,
                            row.try_get::<Option<Vec<String>>, _>(c.ordinal())
                                .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())

View on GitHub (pinned to e29bcd1b41)