polypush135

polypush135

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

Showing Posts 1 to 10

idi527

idi527

:waving_hand:

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 …

polypush135

polypush135 OP

I got that far, though I though it was the same thing I was already doing. That said it does not raise an error, though on the other hand it does not return expected results.

iex(24)> subset_query = from(va in VideoAnalysis,
...(24)>     where: va.id == ^video_id,
...(24)>     limit: 1,
...(24)>     order_by: [desc: :inserted_at]
...(24)>   )
#Ecto.Query<from v in VideoAnalysis, where: v.id == ^3,  order_by: [desc: v.inserted_at], limit: 1>
iex(25)>
nil
iex(26)> Repo.all(
...(26)>   from(c in Clip,
...(26)>       join: va in subquery(subset_query),
...(26)>       join: v in assoc(va, :video)
...(26)>   )
...(26)> )

[debug] QUERY OK source="clips" db=4.4ms queue=0.1ms
SELECT * FROM "video_analyses" AS v0 WHERE (v0."id" = $1) ORDER BY v0."inserted_at" DESC LIMIT 1) AS s1 ON TRUE INNER JOIN "videos" AS v2 ON v2."id" = s1."video_id" [3]
[]
iex(27)>
idi527

idi527

What’s video_id, why is it in both where clauses?

from(q in query,
      join:
        va in ^from(va in VideoAnalysis,
          where: va.id == ^video_id, # here
          limit: 1,
          order_by: [desc: :inserted_at]
        ),
      join: v in assoc(va, :video),
      where: v.id == ^video_id # and here
    )
peerreynders

peerreynders

But there is a catch in my case join: video_analysis in assoc(c, :video_analysis), would return multiple records.

How is that a catch. Given that a video can have multiple clips you would expect the same video analysis to appear with each clip that belongs to the video.

I’m assuming

Clip [0..*] -- [1] Video [1] -- [0..*] VideoAnalysis
polypush135

polypush135 OP

Sorry trial and error copy and pasting

The idea is that I only know of a video but I want all the clips found on the newest video_analysis which belongs to the video. the issue is that there are many video_analysis that belong to a given video

idi527

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
peerreynders

peerreynders

the issue is that there are many video_analysis that belong to a given video

So you want each clip no more than once even if it is referenced by multiple video analyses (for the same video).

A subquery using distinct/3 on the video id could work.

polypush135

polypush135 OP

the stack is like so.

VideoAnalysis:

  • belongs_to :video
  • has_many :clips

Video:

  • has_many :video_analyses

Clip:

  • belongs_to :video_analysis

I want all the clips found on the newest VideoAnalysis that belongs to video x.

LostKobrakai

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
peerreynders

peerreynders

You are starting with a video_id - so ultimately the can only be 1 or 0 distinct video_ids - 0 being the case when you can’t find any VideoAnalyses, 1 if there is one or more Video Analyses.

So it seems to boil down to

  • Give me all video clips if there is at least one Video analysis
  • No clips if no Video analysis can be found

So your query could be phrased as:

  • Give me all the Clips for video_id if there is at least one Video Analysis (… which seems odd)

Something along the lines of

  • Given the set n latest Video Analyses
  • Get me the distinct set of video_ids
  • And get me the associated Clips

sounds much more likely


Never mind, so it was

Video [1] -- [0..*] VideoAnalysis [1] -- [1..*] Clip

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews