Strange error in groupped_by function

#1

Hi.

I got some strange error when I group results. This is probably not due to the consistency of my current database. But I think that even in this case, my code should not cause panic.

// Code
let posts = posts::table.load(&connection)?;
let users_and_posts: Vec<Vec<models::User>> = users::table
            .load(&connection)?
            .grouped_by(&posts);
// Error
    9: <core::option::Option<T>>::expect
                 at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libcore/option.rs:312
      10: <std::collections::hash::map::HashMap<K, V, S> as core::ops::index::Index<&'a Q>>::index
                 at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libstd/collections/hash/map.rs:1658
      11: <Iter as diesel::associations::belongs_to::GroupedBy<'a, Parent>>::grouped_by::{{closure}}
                 at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.3.3/src/associations/belongs_to.rs:128
      12: <core::option::Option<T>>::map
                 at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libcore/option.rs:414
      13: <Iter as diesel::associations::belongs_to::GroupedBy<'a, Parent>>::grouped_by
                 at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.3.3/src/associations/belongs_to.rs:128

Is there any idea how I can fix this?

#2

From looking at the code that causes this panic it seems like your parents (== posts) don’t contain an entity with a id given by your childs (== users).
Without any additional code like your schema definition or the corresponding queryable structs it is quite hard to answer this with more details.