New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`Identifiable` and `BelongsTo` don't compose like `JoinTo` does. #1427

Open
sgrif opened this Issue Dec 20, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@sgrif
Member

sgrif commented Dec 20, 2017

I needed to load some posts, the users who wrote them, the comments left on those posts, and the users who left those comments. Ignoring the "tables can only appear once" restriction (pretend that the second users is avatars or something), I can write this: posts.inner_join(users).inner_join(comments.inner_join(avatars)), which would let me get back (Post, User, (Comment, Avatar). Since I can join from (posts, users) to (comments, avatars), I would expect BelongingToDsl and GroupedBy to behave similarly. We should make this code work (I've added additional intermediate vars and type annotations for clarity):

let posts_and_user: Vec<(Post, User)> = posts::table
    .order(posts::published_at.desc())
    .filter(posts::published_at.is_not_null())
    .inner_join(users::table)
    .load(&conn)?;

let comments_and_user: Vec<(Comment, User)> = Comment::belonging_to(&posts_and_user)
    .inner_join(users::table)
    .load(&conn)?;

let grouped_comments_and_user: Vec<Vec<(Comment, User)>> = comments_and_user
    .grouped_by(&posts_and_user);

let everything: Vec<(Post, User, Vec<(Comment, User)>)> = posts_and_user
    .into_iter()
    .zip(comments_and_user)
    .collect();

@sgrif sgrif added this to the 1.2 milestone Jan 15, 2018

@sgrif

This comment has been minimized.

Member

sgrif commented Jan 19, 2018

There doesn't appear to be an obvious solution here unless we get some changes landed in the language. Attempting to implement Identifiable for tuples just causes Rust to blow up trying to recurse for no reason.

@sgrif sgrif removed this from the 1.2 milestone Feb 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment