I want to have a UUID field as the primary field in a Postgres table, but I get the following error:
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`
Some older questions are there about UUID with diesel but none of them are insertable and I am getting the error specifically in that. I am using diesel with actix web.
Here is my model:
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,
}
And my table schema
table! {
users (id) {
id -> Uuid,
name -> Text,
phone -> Text,
password -> Text,
}
}
I found some old post that suggested that the field might be nullable but id
is the PRIMARY KEY
in my table up.sql, so it is not nullable.
The table is generated from diesel-cli, do there doesn't seem to be any problem there.
Here is my 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"
It's always good to see what's in your Cargo.lock
file and look are pointing for package dependencies.
[[package]]
name = "diesel"
version = "1.4.4"
...
"uuid 0.6.5", <-- It was older version even I've installed newest version of uuid
]
as soon as change features to uuidv07
and run cargo update
Cargo.lock file also changes. If you want to check which Uuid
is applied by diesel. It's possible checking it by looking to the source of the diesel::sql_types::Uuid
to be sure they are using the same versions as uuid::Uuid
.
P.S: Answer was commented by @weiznich in the question post, just adding little bit detailed information and as an answer post whom, who not read comments and look for answer only