UPDATE query_as! with a custom ENUM type (syntax) #3041
-
from migrations 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()
); from models use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Debug, Deserialize, Serialize, Clone, Copy, sqlx::Type, PartialEq, ToSchema)]
#[sqlx(type_name = "user_role", rename_all = "lowercase")]
pub enum UserRole {
Admin,
Moderator,
User,
}
impl UserRole {
pub fn to_str(&self) -> &str {
match self {
UserRole::Admin => "admin",
UserRole::Moderator => "moderator",
UserRole::User => "user",
}
}
}
#[derive(Debug, Deserialize, sqlx::FromRow, sqlx::Type, Serialize, Clone)]
pub struct UserModel {
pub id: uuid::Uuid,
pub name: String,
pub email: String,
pub password: String,
pub role: UserRole,
pub verified: bool,
#[serde(rename = "createdAt")]
pub created_at: Option<DateTime<Utc>>,
#[serde(rename = "updatedAt")]
pub updated_at: Option<DateTime<Utc>>,
} 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 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. 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? |
Beta Was this translation helpful? Give feedback.
Answered by
RAprogramm
Feb 10, 2024
Replies: 1 comment
-
YES! 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""#, |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
RAprogramm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
YES!
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""#,