-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
I have found these related issues/pull requests
Here I am not using a MySQL enum but a VARCHAR field
Description
The sqlx documentation states:
Enumerations may be defined in Rust and can match SQL by integer discriminant or variant name.
With #[repr(_)] the integer representation is used when converting from/to SQL and expects that SQL type (e.g., INT). Without, the names of the variants are used instead and expects a textual SQL type (e.g., VARCHAR, TEXT).
source: https://docs.rs/sqlx/latest/sqlx/trait.Type.html#enumeration
When I implement this in the example below a runtime error is thrown:
thread 'omp_db::test_sqlx_types::test_sqlx_locale' (84932)
panicked at src/omp_db.rs:403:62:
fetched locale: ColumnDecode {
index: "0",
source: "mismatched types;
Rust type `oai_to_alma::omp_db::test_sqlx_types::Locale` (as SQL type `ENUM`)
is not compatible with SQL type `VARCHAR`"
}
What is wrong here?
#[cfg(test)]
mod test_sqlx_types {
use std::sync::LazyLock;
use sqlx::{MySql, Pool, mysql::MySqlPoolOptions};
use crate::config::Config;
pub static DB: LazyLock<Pool<MySql>> = LazyLock::new(|| {
async_std::task::block_on(async {
let url = format!(
"mysql://{}:{}@{}:{}/{}",
// fill in your values when testing
"db.user",
"db.password",
"db.host",
"db.port",
"db.database"
);
MySqlPoolOptions::new()
.max_connections(5)
.connect(&url)
.await
.expect("successfully connected to MySQL OMP database")
})
});
#[derive(Debug, sqlx::Type)]
#[allow(non_camel_case_types)]
pub enum Locale {
de_DE,
en_US,
}
#[test]
fn test_sqlx_locale() {
let query = sqlx::query_scalar(r#"select 'en_US'"#);
let locale: Locale = async_std::task::block_on(query.fetch_one(&*DB))
.expect("fetched locale");
dbg!(locale);
}
}Note: I posted the same issue here: https://stackoverflow.com/questions/79895959/trouble-getting-sqlx-derivesqlxtype-to-work-with-a-rust-enum-using-mysql
Reproduction steps
use the code above and a mysql instance to replicate this.
SQLx version
0.8.6
Enabled SQLx features
"mysql", "runtime-async-std"
Database server and version
mysql Ver 8.0.45-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))
Operating system
Ubuntu 24
Rust version
rustc 1.91.0 (f8297e351 2025-10-28)