gpt4 book ai didi

rust/柴油 : How to query and insert into postgres tables which have uuid

转载 作者:行者123 更新时间:2023-11-29 07:45:43 27 4
gpt4 key购买 nike

我有以下由 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"]

最佳答案

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

您的 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 类型。

关于 rust/柴油 : How to query and insert into postgres tables which have uuid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53326887/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com