15

我有 Diesel 生成的以下模式:

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

和相关的模型

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()
    }
}

当我尝试编译它时出现错误,它说:

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` 

如果我尝试插入,我会得到一个类似的错误,说Expression没有实现。

这可能是我的依赖项有问题,还是我可能忘记添加到模型中?

[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"]
4

2 回答 2

11

刚刚也花了很多时间来解决这个问题。从 Diesel 1.4.5 开始,您可以添加最新版本的uuid,但您必须uuidv07在 Diesel 上指定该功能。

Cargo.toml应该看起来像这样:

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

来源:https ://github.com/diesel-rs/diesel/issues/2348

注意:另外,就像@canab 所说,Uuid结构上的类型应该来自uuid库,而不是 Diesel SQL 类型。

于 2021-02-14T02:37:06.457 回答
10

结构中的类型需要是 Rust 类型而不是 SQL 类型,特别Uuid是来自uuid crate(在 Diesel 1.3 中,Diesel 仅支持 0.6 版本)。在问题的代码中,Uuid扩展为diesel::sql_types::Uuid

#[derive(Queryable)]
pub struct User {
    pub id: uuid::Uuid,
    pub name: String,
}
于 2019-01-18T09:20:45.720 回答