How to check the relationship between the functions in Ecto

You can get all those in one fell swoop with nested preloads.

cms_learn_post -> cms_learn_headlines -> cms_learn_category -> cms_learn_headlines

An equivalent example with

Track -> Album -> Artist -> Album

iex(1)> alias MusicDB.{Repo,Album,Track}
[MusicDB.Repo, MusicDB.Album, MusicDB.Track]
iex(2)> import Ecto.Query
Ecto.Query
iex(3)> track_id = 29
29
iex(4)> query = from(t in Track, [
...(4)>  preload: [{:album,[{:artist,:albums}]}],
...(4)>  where: t.id == ^track_id
...(4)> ])
#Ecto.Query<from t in MusicDB.Track, where: t.id == ^29,
 preload: [album: [artist: [:albums]]]>
iex(5)> Repo.all(query)

17:24:06.532 [debug] QUERY OK source="tracks" db=2.4ms decode=1.6ms
SELECT t0."id", t0."title", t0."duration", t0."index", t0."number_of_plays", t0."inserted_at", t0."updated_at", t0."album_id" FROM "tracks" AS t0 WHERE (t0."id" = $1) [29]
 
17:24:06.538 [debug] QUERY OK source="albums" db=1.6ms
SELECT a0."id", a0."title", a0."inserted_at", a0."updated_at", a0."artist_id", a0."id" FROM "albums" AS a0 WHERE (a0."id" = $1) [4]
 
17:24:06.540 [debug] QUERY OK source="artists" db=0.8ms
SELECT a0."id", a0."name", a0."birth_date", a0."death_date", a0."inserted_at", a0."updated_at", a0."id" FROM "artists" AS a0 WHERE (a0."id" = $1) [2]
 
17:24:06.543 [debug] QUERY OK source="albums" db=1.8ms
SELECT a0."id", a0."title", a0."inserted_at", a0."updated_at", a0."artist_id", a0."artist_id" FROM "albums" AS a0 WHERE (a0."artist_id" = $1) ORDER BY a0."artist_id" [2]
[
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: %MusicDB.Album{
      __meta__: #Ecto.Schema.Metadata<:loaded, "albums">,
      artist: %MusicDB.Artist{
        __meta__: #Ecto.Schema.Metadata<:loaded, "artists">,
        albums: [
          %MusicDB.Album{
            __meta__: #Ecto.Schema.Metadata<:loaded, "albums">,
            artist: #Ecto.Association.NotLoaded<association :artist is not loaded>,
            artist_id: 2,
            genres: #Ecto.Association.NotLoaded<association :genres is not loaded>,
            id: 4,
            inserted_at: ~N[2018-06-16 20:29:41.493342],
            title: "Portrait In Jazz",
            tracks: #Ecto.Association.NotLoaded<association :tracks is not loaded>,
            updated_at: ~N[2018-06-16 20:29:41.493348]
          },
          %MusicDB.Album{
            __meta__: #Ecto.Schema.Metadata<:loaded, "albums">,
            artist: #Ecto.Association.NotLoaded<association :artist is not loaded>,
            artist_id: 2,
            genres: #Ecto.Association.NotLoaded<association :genres is not loaded>,
            id: 3,
            inserted_at: ~N[2018-06-16 20:29:41.483682],
            title: "You Must Believe In Spring",
            tracks: #Ecto.Association.NotLoaded<association :tracks is not loaded>,
            updated_at: ~N[2018-06-16 20:29:41.483713]
          }
        ],
        birth_date: nil,
        death_date: nil,
        id: 2,
        inserted_at: ~N[2018-06-16 20:29:41.481934],
        name: "Bill Evans",
        tracks: #Ecto.Association.NotLoaded<association :tracks is not loaded>,
        updated_at: ~N[2018-06-16 20:29:41.481940]
      },
      artist_id: 2,
      genres: #Ecto.Association.NotLoaded<association :genres is not loaded>,
      id: 4,
      inserted_at: ~N[2018-06-16 20:29:41.493342],
      title: "Portrait In Jazz",
      tracks: #Ecto.Association.NotLoaded<association :tracks is not loaded>,
      updated_at: ~N[2018-06-16 20:29:41.493348]
    },
    album_id: 4,
    duration: 325,
    duration_string: nil,
    id: 29,
    index: 9,
    inserted_at: ~N[2018-06-16 20:29:41.499008],
    number_of_plays: 0,
    title: "Blue In Green",
    updated_at: ~N[2018-06-16 20:29:41.499013]
  }
]
iex(6)>
1 Like