Repository: launchbadge/sqlx

Issue #1171: Add support for enums schema in Postgresql
Created: 2021-04-14T11:48:28Z
Hi!
As far as I can tell, right now it's impossible to use enums in schema other then public.
It would be very helpful if one could specify schema as well, perhaps like this:
#[derive(sqlx::Type)]
#[sqlx(type_name = "scope", schema = "olives")]
pub enum Scope {
    Flowering,
    OilSampling,
    Harvest,
    Structure
}

Duplicate issue #2149: unsupported type ResourceKind for param #2
Created: 2022-10-13T07:02:44Z
Bug Description
my table
CREATE TYPE "ResourceKind" AS ENUM ('MOVIE', 'BOOK');
CREATE TABLE "resources" (
    "id" SERIAL NOT NULL,
    "kind" "ResourceKind" NOT NULL DEFAULT 'BOOK',
    CONSTRAINT "resources_pkey" PRIMARY KEY ("id")
);
    let rec = sqlx::query!(
        r#"
            insert into resources(kind) values($1)
            returning id
        "#,
        kind,
        Local::now().naive_local()
    )
    .fetch_one(pool)
    .await
    .map_err(|_| ServerError::DatabaseFault)?;
it tell me
unsupported type ResourceKind for param #2rustc
mod.rs(319, 9): Actual error occurred here
mod.rs(319, 9): Error originated from macro call here
how can i insert a postgres enum type?

Closing comment (2022-11-04T21:59:49Z): I'm running into this same error but I'm not sure it's related to #1171. My enum is defined inside the same schema as the rest of the database.
CREATE TYPE "role" AS ENUM (
  'user',
  'manager',
  'admin'
);

CREATE TABLE "users" (
  "id" BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  "username" VARCHAR NOT NULL UNIQUE,
  "first_name" VARCHAR,
  "middle_name" VARCHAR,
  "last_name" VARCHAR,
  "email" VARCHAR NOT NULL UNIQUE,
  "role" role NOT NULL,
  "active" BOOLEAN NOT NULL,
  "password_hash" VARCHAR
);
In my code I have:
#[derive(Debug, Default, Clone, Serialize, Deserialize, Type, PartialEq, Eq)]
#[sqlx(rename_all = "snake_case")]
#[sqlx(type_name = "role")]
#[serde(rename_all = "snake_case")]
pub enum Role {
    #[default]
    User,
    Manager,
    Admin,
}


#[derive(Debug, Default, Serialize, Deserialize, FromRow)]
pub struct User {
    #[serde(skip_deserializing)]
    pub id: i64,
    pub username: String,
    pub first_name: Option<String>,
    pub middle_name: Option<String>,
    pub last_name: Option<String>,
    pub email: String,
    pub role: Role,
    pub active: bool,
    password_hash: Option<String>,
}

impl User {
    async fn get_by_username(pool: &PgPool, username: String) -> Result<Self, sqlx::Error> {
        sqlx::query_as!(Self, "SELECT * FROM users WHERE username = $1", username)
            .fetch_one(pool)
            .await
    }
}
Which raises the error:
error: unsupported type role of column #7 ("role")

The only way around this is the much more verbose:
    async fn get_by_username(pool: &PgPool, username: String) -> Result<Self, sqlx::Error> {
        sqlx::query_as!(
            Self,
            r#"
                SELECT 
                    id,
                    username,
                    first_name,
                    middle_name,
                    last_name,
                    email,
                    role as "role: Role",
                    active,
                    password_hash
                FROM users WHERE username = $1
            "#,
            username
        )
        .fetch_one(pool)
        .await
    }