Skip to content

Improve error handling of enums with query_as! macro #2751

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

Open
HarrisonHemstreet opened this issue Sep 14, 2023 · 1 comment
Open

Improve error handling of enums with query_as! macro #2751

HarrisonHemstreet opened this issue Sep 14, 2023 · 1 comment
Labels

Comments

@HarrisonHemstreet
Copy link

HarrisonHemstreet commented Sep 14, 2023

Bug Description

When attempting to execute a SELECT query with the query_as! macro, the macro produces an error when the macro is used on a PostgreSQL table containing a column of type enum.

My understanding is that structs containing enums cannot be used with the query_as! macro and must instead be used with the query_as method. If this is the case a better error message containing something like, "cannot use query_as! macro with PostgreSQL custom types"

Minimal Reproduction

This gist is the main idea. You will have to add the proper imports and such, but the main idea is all here:
https://gist.github.com/HarrisonHemstreet/0d89f2a9bdd257d08a10b7e33c5600ae

use serde::{Deserialize, Serialize};

#[derive(sqlx::Type, Serialize, Deserialize, Debug)]
#[sqlx(type_name = "status", rename_all = "lowercase")]
pub enum Status {
    Active,
    Inactive,
    Suspended
}

#[derive(sqlx::FromRow, Serialize, Deserialize, Debug)]
pub struct TestEnum {
    pub id: Option<i32>,
    pub name: Option<String>,
    pub enum_col: Status,
}

            let returned: Result<TestEnum, Error> = sqlx::query_as!(
            TestEnum,
                r#"SELECT id, name, enum_col FROM test_enum LIMIT 1"#
            )
            .fetch_one(&pg)
            .await;


/*
PostgreSQL:
CREATE TYPE status AS ENUM ('active', 'inactive', 'suspended');

CREATE TABLE test_enum (
  id SERIAL PRIMARY KEY,
  name TEXT,
  enum_col status NOT NULL DEFAULT 'active'
);

insert into test_enum (name)  values ('test');
*/

/*
should produce this error:
```bash
error: unsupported type status of column #3 ("enum_col")
  --> src/routes/auth.rs:62:53
   |
62 |               let returned: Result<TestEnum, Error> = sqlx::query_as!(
   |  _____________________________________________________^
63 | |             TestEnum,
64 | |                 r#"SELECT id, name, enum_col FROM test_enum LIMIT 1"#
65 | |             )
   | |_____________^
   |
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)
*/

Info

  • SQLx version: 0.7.1
  • SQLx features enabled: "postgres", "runtime-tokio", "chrono", "uuid", "macros"
  • Database server and version: Postgres docker hub
  • Operating system: ubuntu 22.04.3 LTS
  • rustc --version: rustc 1.69.0 (84c898d65 2023-04-16)
@TmLev
Copy link

TmLev commented Sep 21, 2023

Have you seen this issue #1004?

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

2 participants