【发布时间】:2020-06-11 23:02:48
【问题描述】:
我想将 UUID 字段作为 Postgres 表中的主字段,但出现以下错误:
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`
有一些关于柴油 UUID 的较早问题,但没有一个是可插入的,我特别收到了错误。我正在使用带有 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,
}
}
我发现一些旧帖子表明该字段可能可以为空,但 id 是我的表 up.sql 中的 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版本之间的版本不匹配。 -
Diesel 目前最新发布的版本不支持
uuid0.8 版,仅支持0.7 及以下版本。此外,您需要使用uuidv07功能来支持 0.7 版。这将随着下一个柴油版本而改变。
标签: postgresql rust rust-diesel actix-web