我正在try 使用Rust中的sqlx::query!
宏将数据插入到PostgreSQL
数据库中.但是,我遇到了一个与uuid::Uuid
类型不满足编码特性相关的编译错误.
Error Message:个
性状界限
uuid::Uuid: Encode<'_, Postgres>
并不满足
Code Snippet:个
use actix_web::{web, HttpResponse};
use sqlx::PgPool;
use uuid::Uuid;
use chrono::Utc;
pub async fn subscribe(form: web::Form<FormData>, connection: web::Data<PgPool>) -> HttpResponse {
sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
"#,
Uuid::new_v4(),
form.email,
form.name,
Utc::now()
)
.execute(connection.get_ref())
.await;
HttpResponse::Ok().finish()
}
Cargo.toml Dependencies:个
[dependencies]
actix-web = "4.4.0"
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio-rustls", "macros", "postgres", "uuid", "chrono"] }
uuid = { version = "0.8.1", features = ["v4", "serde"] }
chrono = "0.4.15"
Attempts to Solve:个
- 我已经确保在sqlx中启用了UUID功能.
- 我try 了不同版本的UUID以判断兼容性.
- 在更改到Cargo.toml后,我运行了干净的Cargo 和Cargo 构建.
What could be causing this trait bound issue and how can I resolve it? Is there a version mismatch, or am I missing a necessary feature activation?个
Update个
我使用此代码更新了代码,但仍显示错误:
use actix_web::{web, HttpResponse};
use chrono::Utc;
use sqlx::types::Uuid;
use sqlx::PgPool;
#[derive(serde::Deserialize)]
pub struct FormData {
email: String,
name: String,
}
pub async fn subscribe(form: web::Form<FormData>, connection: web::Data<PgPool>) -> HttpResponse {
let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?;
sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
"#,
my_uuid,
form.email,
form.name,
Utc::now()
)
// We use `get_ref` to get an immutable reference to the `PgConnection`
// wrapped by `web::Data`.
.execute(connection.get_ref())
.await;
HttpResponse::Ok().finish()
}
它向我显示了这个错误:
let my_uuid = Uuid::parse_str("a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8")?;
?
运算符只能在返回Result
或Option
(或实现FromResidual
的其他类型)的异步函数中使用 特征FromResidual<Result<Infallible, sqlx::types::uuid::Error>>
不是针对HttpResponse
实现