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

标签 postgresql rust rust-diesel actix-web

这个问题在这里已经有了答案:





Why is a trait not implemented for a type that clearly has it implemented?

(1 个回答)



Rust/Diesel: How to query and insert into postgres tables which have uuid

(2 个回答)


去年关闭。




我想要一个 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,
    }
}

我发现一些旧帖子表明该字段可能可以为空,但 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"

最佳答案

看看你的 Cargo.lock 里有什么总是好的file 和 look 指向包依赖项。

[[package]]
name = "diesel"
version = "1.4.4"
...
 "uuid 0.6.5", <-- It was older version even I've installed newest version of uuid
]

一旦将功能更改为 uuidv07并运行 cargo update Cargo.lock 文件也会发生变化。如果您想查看哪个Uuid由柴油应用。可以通过查看 diesel::sql_types::Uuid 的来源来检查它。确保他们使用与 uuid::Uuid 相同的版本.

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

关于postgresql - 不能使用 UUID 作为主键:Uuid:diesel::Expression 不满足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60441600/

相关文章:

asp.net - asp.net 的自定义成员资格提供程序

php - 如何更改 CodeIgniter 中事务的隔离级别?

postgresql - 特性 `diesel::Expression` 没有为 `bigdecimal::BigDecimal` 实现

memory-management - 使用 RefCell 和 Rc 处理循环图中的内存泄漏

rust - 如何使用 Diesel 根据动态参数按列有条件地排序?

rust - 带有 eq_any 的子查询无法编译

sql - 如何使用 PostgreSQL 9.2 计算游戏中每个级别的百分位数

sql - 从表中选择列名和值

rust - Rust 中的惰性序列生成

rust - 不匹配的arm是否会在Rust的 "match"语句中获取变量的所有者?