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

unsupported type ResourceKind for param #2 #2149

Open
kulame opened this issue Oct 13, 2022 · 5 comments
Open

unsupported type ResourceKind for param #2 #2149

kulame opened this issue Oct 13, 2022 · 5 comments
Labels

Comments

@kulame
Copy link

kulame commented Oct 13, 2022

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?

@kulame kulame added the bug label Oct 13, 2022
@kulame
Copy link
Author

kulame commented Oct 14, 2022

#1171 1171

@VersBinarii
Copy link
Contributor

But in your case you should be able to do

let rec = sqlx::query!(
        r#"
            insert into resources(kind) values($1)
            returning id
        "#,
        kind as _,
    )
    .fetch_one(pool)
    .await
    .map_err(|_| ServerError::DatabaseFault)?;

to get it to work

@mike-lloyd03
Copy link

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
    }

@axeld-galadrim
Copy link

@abonander maybe close it as #2149 (comment) solves it ?

@alissa-tung
Copy link

I am confusing about why when using template data, the as _ is needed, and when returning data, role as "role: Role" is needed. Is there any document for this behavior?

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

5 participants