You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The SQL constraints should mean that every session is associated with a user.
I would like to select sessions with their associated user, so that I get (Session, User), but when I use find_also_related() it returns (Session, Option<User>).
Then I looked at the code behind find_also_related and can see it is using a left join:
self.left_join(r).select_also(r)
What I was really looking to do is an inner join:
self.inner_join(r).select_also(r)
But the problem is that the select_also still returns an Option even after doing an inner_join, surely the User can never be None?
It would be great if there could be an API to make this a bit friendlier?
Welcome. I agree we should have an equivalent for inner joins.
The original concern was, there is no simple way to guarantee a related item always exist. With inner join, we can eliminate that concern.
We can do:
have a new selector SelectBoth in which the return type is Vec<(E::Model, F::Model)>
have a new method fn select_both<F>(mut self, _: F) -> SelectBoth<E, F> on Select
have a new method fn inner_join_and_select<R>(self, r: R) -> SelectBoth<E, R> (sadly the name is not consistent with find_xxx_related, but I could not think of a better self-explanatory name)
Motivation
I have a foreign key that looks like:
The SQL constraints should mean that every session is associated with a user.
I would like to select sessions with their associated user, so that I get
(Session, User)
, but when I usefind_also_related()
it returns(Session, Option<User>)
.Then I looked at the code behind
find_also_related
and can see it is using a left join:What I was really looking to do is an inner join:
But the problem is that the
select_also
still returns anOption
even after doing an inner_join, surely the User can never be None?It would be great if there could be an API to make this a bit friendlier?
Additional Information
See also:
The text was updated successfully, but these errors were encountered: