这个问题在这里已经有了答案:
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,
}
}
我发现一些旧帖子表明该字段可能可以为空,但
id
是 PRIMARY 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/