polypush135
Proper way to subquery on a join in ecto
In short this was what I tried to do.
from(q in query,
join:
va in ^from(va in VideoAnalysis,
where: va.id == ^video_id,
limit: 1,
order_by: [desc: :inserted_at]
),
join: v in assoc(va, :video),
where: v.id == ^video_id
)
But soon found that …
** (Ecto.QueryError) ....query.ex:108: queries in joins can only have `where` conditions in query:
from c in Clip,
join: v0 in ^#Ecto.Query<from v in VideoAnalysis, where: v.id == ^7275, order_by: [desc: v.inserted_at], limit: 1>,
join: v1 in assoc(v0, :video),
where: v1.id == ^7275
select: c
trying to figure out how I can query for all the records of a thing through an intermediate record.
IE say I have a VideoAnalysis record that belongs_to a video and has many clips.
How can I query for all the clips of a video analysis if all I know if the video id?
But there is a catch in my case join: video_analysis in assoc(c, :video_analysis), would return multiple records.
Q: How could I make it to where I can limit it to only the newest based on inserted at for VideoAnalysis
in short I’m trying to do all this in the Ecto Query with just one query
if that is even possible
Marked As Solved
LostKobrakai
newest_videoanalysis =
from va in VideAnalysis,
order_by: [desc: va.inserted_at],
where: va.video_id == ^video_id,
limit: 1
from clips in Clips,
join: nva in subquery(newest_videoanalysis), on: clips.video_analysis_id == nva.id
Also Liked
polypush135
Awesome thank you. Yeah I guess I may have caused that to not work with leaving the extra join in that from clips.
idi527
![]()
Have you tried using subquery/2?
latest_video_analysis =
from va in VideoAnalysis,
where: va.id == ^video_id,
limit: 1,
order_by: [desc: :inserted_at]
from v in Video,
join: va in subquery(latest_video_analysis), on: v.id == va.video_id,
where: v.id == ^video_id
Not sure if it would even work …
idi527
What about a subquery in a lateral join then? Ah, it’s not necessary actually. What about this?
latest_video_analysis =
from va in VideoAnalysis,
where: va.video_id == ^video_id,
limit: 1,
order_by: [desc: :inserted_at]
from c in Clip,
join: va in subquery(latest_video_analysis),
where: c.video_analisis_id == va.id
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









