Why I got #Ecto.Association.NotLoaded?

simo picture simo · Jun 1, 2016 · Viewed 12.1k times · Source

I have teams and each team has users, so there is a join table to link users to teams as its a many to many relation, here is my models:

defmodule App.Team do
  use App.Web, :model

  schema "teams" do
    field :owner_id, :integer
    has_many :team_users, {"team_user", App.TeamUser}
  end

end
defmodule App.User do
  use App.Web, :model

  schema "users" do
    # field :email, :string
    has_many :team_user, App.TeamUser
  end
end

And here is the join model:

defmodule App.TeamUser do
  use App.Web, :model

  @primary_key false
  schema "team_user" do
    belongs_to :user, App.User
    belongs_to :team, App.Team
  end

end

If I run a query to get all teams of a user with all resulted teams's users, like this:

teams_users =
      from(t in Team, where: t.owner_id == ^user_id)
      |> Repo.all()
      |> Repo.preload(:team_users)

I get this log:

[%App.Team{__meta__: #Ecto.Schema.Metadata<:loaded>, id: 1,
  is_base_team: true, owner_id: 3,
  team_users: [%App.TeamUser{__meta__: #Ecto.Schema.Metadata<:loaded>,
    team: #Ecto.Association.NotLoaded<association :team is not loaded>,
    team_id: 1,
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    user_id: 3},
   %App.TeamUser{__meta__: #Ecto.Schema.Metadata<:loaded>,
    team: #Ecto.Association.NotLoaded<association :team is not loaded>,
    team_id: 1,
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    user_id: 4},
   %App.TeamUser{__meta__: #Ecto.Schema.Metadata<:loaded>,
    team: #Ecto.Association.NotLoaded<association :team is not loaded>,
    team_id: 1,
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    user_id: 5}]}]

In the log, I got team with id 1, and all its users with ids: (3, 4, 5) But why I got user: #Ecto.Association.NotLoaded<association :user is not loaded>? I didn't ask to load the user at that id any way.. so why I got such warning?

I am using {:phoenix_ecto, "~> 3.0-rc}

Answer

Gazler picture Gazler · Jun 1, 2016

You need to preload the :user as well as the :team_users:

teams_users =
  from(t in Team, where: t.owner_id == ^user_id)
  |> Repo.all()
  |> Repo.preload(team_users: :user)

There is a section on nested associations in the docs. https://hexdocs.pm/ecto/Ecto.Query.html#preload/3