jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

I NEED YOUR HELP!!! query_as! UPDATE ... SET ...RETURNING...(unsupported type user_role for param #1) #911

Closed RAprogramm closed 9 months ago

RAprogramm commented 9 months ago

i have the next one

    async fn update_user<T: Into<String> + Send>(
        &self,
        user_id: Option<Uuid>,
        role: T,
    ) -> Result<Option<UserModel>, Error> {
        let now = chrono::Utc::now();

        if let Some(user_id) = user_id {
            let existing_user = sqlx::query_as!(
            UserModel,
            r#"SELECT id,name,email,password,verified,created_at,updated_at,role as "role: UserRole" FROM users WHERE id = $1"#,
            user_id
            )
            .fetch_optional(&self.pool)
            .await?;

            if existing_user.is_none() {
                return Err(Error::RowNotFound);
            }

            let updated_user = sqlx::query_as!(
                UserModel,
                r#"UPDATE users SET (role,updated_at) VALUES ($1,$2) WHERE id=$3 RETURNING id,name,email,verified,created_at,password,updated_at,role as "role: UserRole""#,
                role.into(),
                now,
                user_id,
            )
            .fetch_optional(&self.pool)
            .await?;

            return Ok(updated_user);
        }

        Err(sqlx::Error::RowNotFound)
    }

my problem in updated_user query. I didn't find any documentation about UPDATE query with ENUM in db.

CREATE TYPE user_role AS ENUM ('admin', 'moderator', 'user');

CREATE TABLE "users" (
    id UUID NOT NULL PRIMARY KEY DEFAULT (uuid_generate_v4()),
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    verified BOOLEAN NOT NULL DEFAULT FALSE,
    password VARCHAR(100) NOT NULL,
    role user_role NOT NULL DEFAULT 'user',
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

this is migration file.

 let updated_user = sqlx::query_as!(
                UserModel,
                r#"UPDATE users SET (role,updated_at) VALUES ($1,$2) WHERE id=$3 RETURNING id,name,email,verified,created_at,password,updated_at,role as "role: UserRole""#,
                role.into(),
                now,
                user_id,
            )
            .fetch_optional(&self.pool)
            .await?;

i know that my problem is wrong query syntax. i'he already tried this

UPDATE users SET role=$1,updated_at=$2 WHERE id=$3 RETURNING id,name,email,verified,created_at,password,updated_at,role as "role: UserRole""

this

UPDATE users SET role=$1,updated_at$2, role as "role:UserRole" WHERE id=$3 RETURNING id,name,email,verified,created_at,password,updated_at,role as "role: UserRole""

this

UPDATE users SET (role,updated_at) VALUES ($1,$2) WHERE id=$3 RETURNING id,name,email,verified,created_at,password,updated_at,role as "role: UserRole""

and many other variations

Maybe someone can shed some light on this situation?

griggsca91 commented 9 months ago

I think your code has some syntax errors. Doesn't look like it would compile with Go :)

i believe you meant to put an issue on this project - https://github.com/launchbadge/sqlx

RAprogramm commented 9 months ago

I think your code has some syntax errors. Doesn't look like it would compile with Go :)

i believe you meant to put an issue on this project - https://github.com/launchbadge/sqlx

Thank you for bringing it to my attention! turns out I created in the wrong repository.

but if you have any ideas on how to make a request correctly, let me know

RAprogramm commented 9 months ago

YES! I FOUND SOLUTION!

here is right query syntax:

r#"UPDATE users SET role=($1::text)::user_role, updated_at=$2 WHERE id=$3 RETURNING id, name, email, verified, created_at, password, updated_at, role as "role:UserRole""#,