Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLX Mysql Enum decode error #1379

Open
amitavaghosh1 opened this issue Aug 16, 2021 · 4 comments
Open

SQLX Mysql Enum decode error #1379

amitavaghosh1 opened this issue Aug 16, 2021 · 4 comments

Comments

@amitavaghosh1
Copy link

amitavaghosh1 commented Aug 16, 2021

 ColumnDecode { index: "\"template_type\"", source: "mismatched types; Rust type `template::app::model::TemplateType` (as SQL type `ENUM`) is not compatible with SQL type `ENUM`" }
#[derive(Serialize, Deserialize, Clone, Debug, EnumString, ToString, Type)]
// #[sqlx(rename="template_type", rename_all = "snake_case")] 
pub enum TemplateType {
    Email,
    Pns
}


#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Template {
    pub tname: String,
    pub template_type: TemplateType,
    pub created_at: DateTime::<Utc>
}
@amitavaghosh1
Copy link
Author

amitavaghosh1 commented Aug 16, 2021

One way to solve this is to overwrite the FromRow trait as such.

impl <'r>FromRow<'r, MySqlRow> for Template {
    fn from_row(row: &'r MySqlRow) -> Result<Self, MysqlError> {
        let tt: String = row.try_get("template_type")?;

        let template_type = match TemplateType::from_str(&tt) {
                Ok(t) => t,
                Err(_) => TemplateType::Email,
        };

        return Ok(Template{
            tname: row.try_get("tname")?,
            template_type: template_type,
            created_at: row.try_get("created_at")?
        });
    }
}

The to_string and from_str are methods from strum package, which converts enums to string and vice versa.

@amitavaghosh1 amitavaghosh1 changed the title Mysql Enum decode error SQLX Mysql Enum decode error Aug 16, 2021
@bin
Copy link

bin commented Oct 23, 2021

I am experiencing the same issue; I've added #[repr(u8)] to my enums and represent them as TINYINTs instead in MariaDB as a temporary fix.

@ultimaweapon
Copy link

ultimaweapon commented Oct 29, 2024

Just stumble upon this issue with 0.8.2. It seems like this should be fixed by #3371, which included in the 0.8.2 but somehow it does not work for me.

@ultimaweapon
Copy link

After digging into this found out https://github.com/launchbadge/sqlx/blob/main/sqlx-mysql/src/type_info.rs#L109 is a problem. It works if the column is nullable and have a default value otherwise it will fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants