我希望在Postgres表中将UUID字段作为主键,但是我遇到了以下错误:
error[E0277]: the trait bound `uuid::Uuid: diesel::Expression` is not satisfied
--> database/src/models.rs:3:35
|
3 | #[derive(Debug, Clone, Queryable, Insertable)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `uuid::Uuid`
|
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Uuid>` for `uuid::Uuid`
有一些关于在diesel中使用UUID的旧问题,但它们都无法插入数据,并且我正面临这个具体的错误。我正在使用diesel和actix web。
这是我的模型:
use crate::schema::users;
#[derive(Debug, Clone, Queryable, Insertable)]
#[table_name="users"]
pub struct User {
pub id: uuid::Uuid,
pub phone: String,
pub name: String,
pub password: String,
}
我的数据库表结构
table! {
users (id) {
id -> Uuid,
name -> Text,
phone -> Text,
password -> Text,
}
}
我找到了一些旧帖子,它们表明该字段可能是可空的,但在我的表up.sql中,id
是PRIMARY KEY
,因此它不可为空。
这个表是从diesel-cli生成的,所以似乎没有任何问题。
这是我的Cargo.toml文件:
diesel = { version = "1.0.0", features = ["postgres", "r2d2", "uuid"] }
uuid = { version = "0.8", features = ["v4"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
diesel
和uuid
版本的信息?另外,你为diesel
启用了哪些特性标志?很可能是由于缺少特性标志或者uuid
版本不匹配导致的问题。请注意检查diesel
内部使用的uuid
版本和你自己使用的uuid
版本是否一致。 - weiznichuuid
的 0.8 版本,只支持 0.7 及以下版本。此外,你需要使用uuidv07
功能来支持 0.7 版本。这将在下一个 Diesel 发布中改变。 - weiznich