Skip to content

Return Vec<Option<Type> with belonging_to #4402

Closed Answered by weiznich
RubberDuckShobe asked this question in Q&A
Discussion options

You must be logged in to vote

The correct way to model an 1:1 relation is using a join. For optional 1:1 dependencies that means you want to use left_join().

In this particular case you end up with a query like that:

songs::table.left_join(extra_song_info::table).filter(songs::id.eq_any(song_ids)).select(Option::<ExtraSongInfo>::as_select()).load(&mut conn)?;

See the Relation Guide for more details and information on this topic.

Side note: When I omit .optional(), Visual Studio Code (or rust-analyzer itself?) tries telling me that extra_info is Vec<Option>, even though it's actually Vec.

That's an issue in rust-analyzer, not in diesel. Please report it in their issue tracker, as we cannot do much about it.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by RubberDuckShobe
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants