Support alias for enum values
I have found these related issues/pull requests
There is an issue about supporting alias on columns for joined tables, but that appears to be unrelated to the present feature request.
Description
I am transitioning with a large legacy postgresql schema from typescript to rust/sqlx. In the schema, I have a dataset(id, "dataType") table, where dataType is a TEXT column with entries DataType.Unknown and DataType.Image, which I'm mapping to an enum like so:
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, sqlx::Type)]
#[sqlx(type_name = "\"DataType\"")]
pub enum DataType {
#[sqlx(rename = "DataType.Unknown")]
Unknown = 0,
#[sqlx(rename = "DataType.Image")]
Image = 1,
}
let result = sqlx::query!(r#"SELECT id, "dataType: DataType" FROM dataset LIMIT 10;"#).execute(db).await;
While this approach appears to be working fine in most cases, some legacy developer at some point added unexpected None entries to the column, which causes a runtime error in the query: invalid value \"None\" for enum DataType.
Prefered solution
While I could add a new DataType::None to the enum, I would like to avoid that because it is against the defined spec. Instead, I would love to add an alias so that either DataType.Unknown | None decode to the same DataType::Unknown value.
In serde, I can do that with:
pub enum DataType {
#[serde(rename = "DataType.Unknown", alias = "None")]
Unknown,
...
}
However, alias is not currently available in sqlx.
- Is that something that could be added to cover such legacy use-cases in the future?
- Is there any available workaround to decode both values to
DataType::Unknown? Upd: I can manually implement theDecode/Encode/TypeInfotraits, which worked at the cost of significant amount of boilerplate.
Is this a breaking change? Why or why not?
Probably not.