rust-diesel

Writing Diesel CRUD operations for generic types

别说谁变了你拦得住时间么 提交于 2021-02-18 17:34:37
问题 I am trying to write a Rust crate which removes some boilerplate code from the user when creating simple CRUD operations with Diesel For instance, if you have a Diesel Insertable like this one: #[derive(Insertable)] #[table_name = "users"] pub struct UserCreate<'a> { pub email: String, pub hash: &'a [u8], pub first_name: Option<String>, pub family_name: Option<String>, } I want the crate user to just write create<UserCreate>(model, pool) , to insert the struct fields into a database row. To

How do I conditionally order by a column based on a dynamic parameter with Diesel?

那年仲夏 提交于 2021-02-10 14:18:12
问题 I'm trying to specify different columns for order_by depending on an external parameter. This works, but is ugly: #[macro_use] extern crate diesel; use crate::diesel::prelude::*; use diesel::pg::PgConnection; mod schema { table! { items (id) { id -> Int4, name -> Text, } } } #[derive(Queryable, Debug)] pub struct Item { pub id: i32, pub name: String, } fn load_items(conn: PgConnection, sort_prop: String, sort_dir: String) -> Vec<Item> { use schema::items::dsl::*; let mut query = items.into

Why do I get “overflow evaluating the requirement” when rewriting a function using Diesel's traits into a trait method?

萝らか妹 提交于 2021-02-08 05:16:34
问题 I am trying to add pagination using Diesel. The compiler is able to check bounds on a generic type if I use a function but isn't if I try to do the same as an implementation of a trait. This is a simple working example: use diesel::query_dsl::methods::{LimitDsl, OffsetDsl}; pub fn for_page<T>(query: T) where T: OffsetDsl, T::Output: LimitDsl, { query.offset(10).limit(10); } OffsetDsl and LimitDsl are Diesel's traits which provides the methods offset and limit . When I try to extract this

Why do I get “overflow evaluating the requirement” when rewriting a function using Diesel's traits into a trait method?

断了今生、忘了曾经 提交于 2021-02-08 05:14:45
问题 I am trying to add pagination using Diesel. The compiler is able to check bounds on a generic type if I use a function but isn't if I try to do the same as an implementation of a trait. This is a simple working example: use diesel::query_dsl::methods::{LimitDsl, OffsetDsl}; pub fn for_page<T>(query: T) where T: OffsetDsl, T::Output: LimitDsl, { query.offset(10).limit(10); } OffsetDsl and LimitDsl are Diesel's traits which provides the methods offset and limit . When I try to extract this

Cannot infer type for `U`

拈花ヽ惹草 提交于 2021-02-07 06:49:13
问题 I am using Rust and Diesel: fn create_asset_from_object(assets: &HashMap<String, Assets_Json>) { let connection: PgConnection = establish_connection(); println!("=========================================================="); insert_Asset(&connection, &assets); } pub fn insert_Asset(conn: &PgConnection, assests: &HashMap<String, Assets_Json>){ use self::schema::assets; for (currency, assetInfo) in assests { let new_asset = self::models::NewAssets { asset_name: &currency, aclass: &assetInfo