不能使用 UUID 作为主键:Uuid:diesel::Expression 不满足

Ayu*_*ngh 3 postgresql rust rust-diesel actix-web

我想要一个 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`
Run Code Online (Sandbox Code Playgroud)

有一些关于带有柴油的 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,
}
Run Code Online (Sandbox Code Playgroud)

还有我的表架构

table! {
    users (id) {
        id -> Uuid,
        name -> Text,
        phone -> Text,
        password -> Text,
    }
}
Run Code Online (Sandbox Code Playgroud)

我发现了一些老的职位,建议字段可能可空,但是idPRIMARY KEY我的表up.sql,所以它不能为空。

该表是由柴油 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"
Run Code Online (Sandbox Code Playgroud)

sam*_*bua 6

查看Cargo.lock文件中的内容并查看指向包依赖项的内容总是很好的。

[[package]]
name = "diesel"
version = "1.4.4"
...
 "uuid 0.6.5", <-- It was older version even I've installed newest version of uuid
]
Run Code Online (Sandbox Code Playgroud)

一旦更改功能uuidv07并运行cargo updateCargo.lock 文件也会更改。如果您想检查Uuid柴油应用哪个。可以通过查看 的来源来检查它,以diesel::sql_types::Uuid确保它们使用与uuid::Uuid.

PS:答案是@weiznich在问题帖中评论的,只是添加了一点详细信息,作为答案帖谁不看评论只找答案