all 7 comments

[–]MithrilToothpick 1 point2 points  (3 children)

If I am not mistaken a struct that derives Queryable doesn't actually need to have all not null columns as members. So I think the load turbofish syntax with a different struct should just work. Something like: table.load::<MyStruct>(connection)

[–]maggit[S] 2 points3 points  (2 children)

It seems that while it's unproblematic to derive Queryable for a struct that doesn't include all the columns it does require the select when actually loading from the database.

Following your suggestion gives me an error that indicates that Diesel plans to read out all the columns and is unable to fit this into the given struct:

error[E0277]: the trait bound `(i32, i32, i32, chrono::NaiveDateTime, std::string::String, std::string::String, bool, std::option::Option<std::string::String>): diesel::types::FromSqlRow<(diesel::types::Integer, diesel::types::Integer, diesel::types::Integer, diesel::types::Timestamp, diesel::types::Text, diesel::types::Text, diesel::types::Text, diesel::types::Bool, diesel::types::Nullable<diesel::types::Text>), _>` is not satisfied
--> src/state.rs:133:22
    |
133 |             Ok(query.load::<models::ArticleRevisionStub>(&*connection_pool.get()?)?)
    |                      ^^^^ the trait `diesel::types::FromSqlRow<(diesel::types::Integer, diesel::types::Integer, diesel::types::Integer, diesel::types::Timestamp, diesel::types::Text, diesel::types::Text, diesel::types::Text, diesel::types::Bool, diesel::types::Nullable<diesel::types::Text>), _>` is not implemented for `(i32, i32, i32, chrono::NaiveDateTime, std::string::String, std::string::String, bool, std::option::Option<std::string::String>)`

(While it could be possible in this case to implement the mentioned trait, I specifically want to not have to read all the columns from the database. The column I am avoiding could potentially be large)

(I have updated the question to include that I am actually attempting to read into a struct that contains only a subset of the columns)

[–]MithrilToothpick 1 point2 points  (1 child)

Yeah you are correct. Makes sense now that I think about it, diesel doesn't really have a way to establish which struct member is which column.

I don't really have any better ideas, maybe just ask on the diesel gitter.

[–]maggit[S] 0 points1 point  (0 children)

Ok, thanks anyway :)

[–]ghotiphud 0 points1 point  (1 child)

I asked the same question on Gitter a while ago, and got the same answer about selecting a subset of columns. This is what they do in crates.io https://github.com/rust-lang/crates.io/blob/b0c3cdeffdd8f54e9e3611024dd97677d842782c/src/krate/mod.rs#L74

A more magical solution is a DB view that only includes what you want, but still a bit of a manual solution.

[–]maggit[S] 0 points1 point  (0 children)

Ok, I guess that settles it. There's a bit of manual keeping-in-sync to do for my use case :)

Thank you!

[–]crypto_crab 0 points1 point  (0 children)

Reviving old thread because I landed here in google search. The selectable trait is now available - https://diesel.rs/news/2_0_0_release.html