Rust / Diesel:如何查询具有uuid的postgres表并将其插入

时间:2018-11-15 19:46:08

标签: rust rust-diesel

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

2 个答案:

答案 0 :(得分:5)

刚刚也花了很多时间解决这个问题。看起来从 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 类型。

答案 1 :(得分:2)

结构中的类型必须是Rust类型而不是SQL类型,特别是uuid crate中的// Adding Click Listener popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.latest: // Handle Latest Click mSortingTextView.setText("Latest"); if (mTrips.size() > 0) { Collections.sort(mTrips, Trip.DATE_NEW_TO_OLD); Collections.sort(mTripListRecyclerViewAdapter.mTripsFiltered, Trip.DATE_NEW_TO_OLD); } mTripListRecyclerViewAdapter.notifyDataSetChanged(); return true; case R.id.price_low_to_high: // Handle Price (Low to High) Click mSortingTextView.setText("Price (Low to High)"); if (mTrips.size() > 0) { Collections.sort(mTrips, Trip.PRICE_LOW_TO_HIGH); Collections.sort(mTripListRecyclerViewAdapter.mTripsFiltered, Trip.PRICE_LOW_TO_HIGH); } mTripListRecyclerViewAdapter.notifyDataSetChanged(); return true; case R.id.price_high_to_low: // Handle Price (High to Low) Click mSortingTextView.setText("Price (High to Low)"); if (mTrips.size() > 0) { Collections.sort(mTrips, Trip.PRICE_HIGH_TO_LOW); Collections.sort(mTripListRecyclerViewAdapter.mTripsFiltered, Trip.PRICE_HIGH_TO_LOW); } mTripListRecyclerViewAdapter.notifyDataSetChanged(); return true; case R.id.date_new_to_old: // Handle Date (New to Old) Click mSortingTextView.setText("Date (New to Old)"); if (mTrips.size() > 0) { Collections.sort(mTrips, Trip.DATE_NEW_TO_OLD); Collections.sort(mTripListRecyclerViewAdapter.mTripsFiltered, Trip.DATE_NEW_TO_OLD); } mTripListRecyclerViewAdapter.notifyDataSetChanged(); return true; default: return false; } } }); (在Diesel 1.3中,Diesel仅支持0.6版本)。在问题代码中,Uuid扩展为Uuid

diesel::sql_types::Uuid