Quick Fix: Postgres decoding for ActiveEnum rs_type Text#3011
Quick Fix: Postgres decoding for ActiveEnum rs_type Text#3011sinder38 wants to merge 2 commits intoSeaQL:masterfrom
Conversation
|
@Huliiiiii, Why:The rs_type = "String" is the core issue as sea-orm forces all string enums through string at the sqlx layer, losing the type name information at compile time. impl TryGetable for Tea {
fn try_get_by(res, idx) {
let value = String::try_get_by(res, idx)?; //forces through String
Tea::try_from_value(&value)
}
}String::type_info() returns PgTypeInfo::TEXT, not PgTypeInfo::with_name("tea"). So compatible() sees "TEXT" != "tea" and so fails. The proper compile-time fix would be:The DeriveActiveEnum macro should generate an additional impl when db_type = "Enum" (it's a native PG enum, not a String enum): // So, for sqlx-postgres only, when db_type = "Enum":
#[cfg(feature = "sqlx-postgres")]
impl sqlx::Type<sqlx::Postgres> for Tea {
fn type_info() -> sqlx::postgres::PgTypeInfo {
sqlx::postgres::PgTypeInfo::with_name("tea")
}
}
#[cfg(feature = "sqlx-postgres")]
impl<'r> sqlx::Decode<'r, sqlx::Postgres> for Tea {
fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result<Self, ...> {
let s = <&str as sqlx::Decode<sqlx::Postgres>>::decode(value)?;
Tea::try_from_value(&s.to_owned()).map_err(|e| e.to_string().into())
}
}And then TryGetable for Tea on postgres would call row.try_get::() directly without string intermediate. No runtime check, no try_get_unchecked. |
|
Yes, I’m trying this solution. |
|
The fix has been added in #2955. |
PR Info
enumfield withfind_by_statement#2423