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"