Extending the DSL to allow a JOIN on a subselection #4444
Unanswered
ginger51011
asked this question in
Q&A
Replies: 1 comment 1 reply
-
This particular part of the query builder is currently hard to extend. We are open to suggestions how to improve that, but so far that hasn't been a priority to research for us. That written: You can likely restructure your query to use a subquery instead of the join, so something like: SELECT * FROM A INNER JOIN B ON A.id = B.a_id WHERE A.id = (SELECT other_B.a_id FROM B AS other_B WHERE B.id = other_b.id ORDER BY other_b.inserted_at DESC LIMIT 1) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I'm working on a feature and I think I know what I want to happen, but not how to express it in diesel.
Basically, I have tables
A
andB
, whereB
has many-to-one relationship forA
. I want to be able to join only the latest value inB
toA
(if it exists), and then to be able to do filtering etc. on that result. The reason for not just using raw SQL is that I want to augment the real query depending on some external parameters.From what I can read in the extending diesel guide, I might want to do something like this:
and then for example
So I guess I want to express that
LatestBJoined
can be handled just like aLeftJoin<A, B>
. Can this be done, and if so how?Please point out if I'm going down the wrong path. I was thinking about just saving
latest_b
as a view and select from that, but it seems like diesel does not support views so that is also not that great.This discussion about greatest-n-per-group might be related, but there is a mention about extending the DSL there as well but I can't figure out how to do it properly.
Beta Was this translation helpful? Give feedback.
All reactions