Skip to content

Validate Enums at compile time #3792

@Gioppix

Description

@Gioppix

I have found these related issues/pull requests

none

Description

Since query! needs enums types to be overridden, compile time checks (for that column) are disabled.

Example:

#[sqlx(type_name = "task_type", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum TaskKind {
    SendMessage,
    Wait,
    SendMail,
    Filter,
    EditTag,
}


let test = query!(
    r#"
    SELECT exec_id, type as typee
    FROM funnel_queue JOIN funnel_steps on funnel_queue.step_id = funnel_steps.step_id
    LIMIT 1;
    "#
)

This throws a compile time error: rustc: unsupported type task_type of column #6 ("typee").
It must be solved with a as "typee: TaskKind".

Prefered solution

I read that it's difficult to fix this directly; my proposal is to still validate the enum at compile time to at least know that the type is correct. Since there's a type_name = "task_type" this should be possible.

Is this a breaking change? Why or why not?

Could make builds fail if someone has a mismatching enum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions