19

I have the following schema generated by Diesel:

table! {
user (id) {
    id -> Uuid,
    name -> Text
}

and the associated model

use diesel::{
    self,
    Queryable,
    Insertable,
};
use diesel::prelude::*;
use diesel::sql_types::Uuid;
use super::schema::user;

#[derive(Queryable)]
pub struct User {
    pub id: Uuid,
    pub name: String,
}

impl User {

    pub fn get(id: i32, connection: &PgConnection) -> Vec<User> {
        user::table.load::<User>(connection).unwrap()
    }
}

I get an error when I try to compile this which says:

21 |         user::table.load::<User>(connection).unwrap()                                                                                                                              
   |                         ^^^^ the trait `diesel::Queryable<diesel::sql_types::Uuid, diesel::pg::Pg>` is not implemented for `diesel::sql_types::Uuid` 

If I try to insert I get a similar error saying that Expression is not implemented.

Could this be a problem with my dependencies or something I may have forgotten to add to the model?

[dependencies]
rocket = "0.4.0-rc.1"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
diesel = { version = "1.0.0", features = ["postgres", "uuid"] }
r2d2 = "*"
r2d2-diesel = "*"

[dependencies.rocket_contrib]
version = "0.4.0-rc.1"
default-features = false
features = ["json", "diesel_postgres_pool", "uuid"]
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jamie Davenport
  • 351
  • 3
  • 10

2 Answers2

17

Just spent a lot of time with this problem as well. It looks like as of Diesel 1.4.5, you can add the latest version of uuid, but you have to specify the uuidv07 feature on Diesel.

Your Cargo.toml should look something like this:

uuid = { version = "0.8.2", features = ["serde", "v4"] }
diesel = { version = "1.4.5", features = ["chrono", "postgres", "r2d2", "uuidv07"] }

Source: https://github.com/diesel-rs/diesel/issues/2348

Note: Also, like @canab said, the Uuid type on the struct should be from the uuid library, not the Diesel SQL type.

danitrod
  • 401
  • 5
  • 10
12

The type in the struct needs to be a Rust type rather than a SQL type, specifically Uuid from the uuid crate (in Diesel 1.3, only version 0.6 is supported by Diesel). In the code from the question, the Uuid is expanded to a diesel::sql_types::Uuid

#[derive(Queryable)]
pub struct User {
    pub id: uuid::Uuid,
    pub name: String,
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
canab
  • 121
  • 1
  • 3
  • 5
    After reading just about everything I could find online, this issue was resolved for me finally by fixing `uuid` to version 0.6. Thank you for pointing this out, it wasn't made obvious anywhere else. – ajxs Apr 28 '19 at 08:26
  • 1
    In my case I just change from `uuid` to `uuidv07` on features for diesel – Eduardo Pereira Jun 29 '21 at 21:20