Runtime decoding error when mapping MariaDB `YEAR`/`SMALLINT` to `u16` using `query_as!`
I have found these related issues/pull requests
I encountered an unexpected runtime decoding error while using sqlx::query_as! with MariaDB.
The application compiled successfully, but at runtime failed with:
error occurred while decoding column N:
out of range integral type conversion attemptedThe affected column was either a MariaDB YEAR or SMALLINT column mapped to a Rust u16.
At the time the issue occurred, adding the following derives to the destination struct made the runtime error disappear:
#[derive(Debug, Serialize, ToSchema, Clone, sqlx::FromRow)]Although query_as! should not require FromRow, and neither Clone nor FromRow should affect primitive value decoding, the behavior changed after adding these derives.
Unfortunately, I have not yet been able to construct a minimal reproducible example, so I cannot determine whether these derives are the root cause or whether they only changed the generated code path.
Query
sqlx::query_as!(
Class,
"SELECT id, major_id, name, enrollment_year, created_at, updated_at
FROM classes
WHERE id = ?",
id
)
.fetch_one(pool)
.awaitExpected Behavior
The runtime decoding behavior of query_as! should not depend on unrelated derives such as Clone or sqlx::FromRow.
Notes
I understand that this report is currently incomplete because I cannot provide a reliable minimal reproduction.
I'm opening this issue mainly to ask whether this behavior is already known or whether there are existing issues involving MariaDB, query_as!, and decoding YEAR/SMALLINT into u16.
If needed, I'd be happy to continue investigating and provide a minimal reproducible example.
Description
I encountered an unexpected decoding issue when using sqlx::query_as! with MariaDB.
When selecting a YEAR or SMALLINT column into a Rust u16 field, the query fails with:
error occurred while decoding column N:
out of range integral type conversion attemptedHowever, after deriving both:
#[derive(sqlx::FromRow, Clone)]for the destination struct, the exact same query succeeds without changing:
- SQL
- database schema
- database contents
- field types
This behavior is unexpected because query_as! should not require FromRow, and Clone should not affect primitive value decoding.
I understand that this report is currently incomplete because I cannot provide a reliable minimal reproduction.
I'm opening this issue mainly to ask whether this behavior is already known or whether there are existing issues involving MariaDB, query_as!, and decoding YEAR/SMALLINT into u16.
If needed, I'd be happy to continue investigating and provide a minimal reproducible example.
Reproduction steps
The destination struct was similar to:
type Id = u64;
#[derive(Debug, Serialize, ToSchema)]
pub struct Class {
pub id: Id,
pub major_id: Id,
pub name: String,
pub enrollment_year: u16,
pub created_at: OffsetDateTime,
pub updated_at: OffsetDateTime,
}After changing it to:
type Id = u64;
#[derive(Debug, Serialize, ToSchema, Clone, sqlx::FromRow)]
pub struct Class {
pub id: Id,
pub major_id: Id,
pub name: String,
pub enrollment_year: u16,
pub created_at: OffsetDateTime,
pub updated_at: OffsetDateTime,
}the runtime error disappeared.
SQLx version
0.8.6
Enabled SQLx features
mysql, runtime-tokio, macros, time
Database server and version
10.6.23-MariaDB-0ubuntu0.22.04.1
Operating system
ArchLinux
Rust version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
Source: transact-rs/sqlx