我正在尝试在Postgres中创建一个自定义的枚举类型,并且已经成功完成了。我的迁移代码如下:
然后我在Rust中创建了这样的枚举类型:
但是现在当我尝试像这样查询数据库时:
我遇到了这个错误:
我已经尝试过调整宏部分
但似乎没有起到帮助作用。
这是来自
CREATE TYPE role AS ENUM ('admin', 'user');
ALTER TABLE users
ADD role role DEFAULT 'user';
然后我在Rust中创建了这样的枚举类型:
#[derive(Serialize, Deserialize, Debug, sqlx::Type)]
#[sqlx(type_name = "role", rename_all = "lowercase")]
pub enum Role {
ADMIN,
USER
}
而且我也修改了用户模型:
#[derive(sqlx::FromRow, Debug)]
pub struct User {
pub id: i32,
pub email: String,
pub username: String,
pub password: String,
pub role: Role,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
但是现在当我尝试像这样查询数据库时:
let user = match sqlx::query_as!(
User,
"SELECT * FROM users WHERE email = $1 AND password = $2",
&email,
&password,
)
我遇到了这个错误:
不支持列#7(“角色”)的类型
我做错了什么?我已经尝试过调整宏部分
#[sqlx(type_name = "role", rename_all = "lowercase")]
,但似乎没有起到帮助作用。
这是来自
cargo check
的完整错误信息:error: unsupported type role of column #7 ("role")
--> src/routes/auth/mod.rs:140:20
|
140 | let user = match sqlx::query_as!(
| __________________________^
141 | | User,
142 | | "SELECT * FROM users WHERE email = $1 AND password = $2",
143 | | &payload.email,
144 | | &hash,
145 | | )
| |_________^
|
= 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)
error: could not compile `rust-api-example` (bin "rust-api-example") due to previous error
cargo check
的完整错误信息是什么? - undefined