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::Type for Enum with String repr #3630

Open
tisonkun opened this issue Dec 7, 2024 · 3 comments
Open

sqlx::Type for Enum with String repr #3630

tisonkun opened this issue Dec 7, 2024 · 3 comments
Labels

Comments

@tisonkun
Copy link

tisonkun commented Dec 7, 2024

Bug Description

Currently, postgres Type would always generate Type with PgTypeInfo::with_name(#ty_name) for enum.

While we are using sqlx::Type derive with Enum and the underline type is expected to be TEXT or VARCHAR, this can result in an error:

error returned from database: type "datatype" does not exist

I suppose we can suppress this generation or use TEXT with some config options.

Minimal Reproduction

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, sqlx::Type)]
#[serde(rename_all = "snake_case")]
#[sqlx(rename_all = "snake_case")]
pub enum DataType {
    Int,
    UInt,
    Float,
    Binary,
    String,
    Boolean,
    Interval,
    Timestamp,
    Variant,

    Null,
}

And try to insert into a DB with column data_type TEXT.

Info

  • SQLx version: 0.8.2
  • SQLx features enabled: ["postgres", "runtime-tokio-rustls"]
  • Database server and version: [REQUIRED] Postgres 16.3
  • Operating system: macOS 13.4
  • rustc --version: rustc 1.83.0-nightly (363ae4188 2024-09-24)
@tisonkun tisonkun added the bug label Dec 7, 2024
@tisonkun
Copy link
Author

tisonkun commented Dec 7, 2024

Straightforward, render type with TEXT if #[sqlx(type_name = "...")] is not specified, instead of default to ident's name.

@tisonkun
Copy link
Author

tisonkun commented Dec 7, 2024

Use #[sqlx(type_name = "TEXT")] seems can work it around.

@tisonkun
Copy link
Author

tisonkun commented Dec 7, 2024

I noticed the "the names of the variants are used" in https://docs.rs/sqlx/latest/sqlx/trait.Type.html#enumeration

Without, the names of the variants are used instead and expects a textual SQL type (e.g., VARCHAR, TEXT).

now.

So this can be a feature request rather than a bug.

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

No branches or pull requests

1 participant