Elixir 为什么我没有上膛?
我有团队,每个团队都有用户,因此有一个连接表将用户链接到团队,因为它是多对多关系,下面是我的模型:Elixir 为什么我没有上膛?,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我有团队,每个团队都有用户,因此有一个连接表将用户链接到团队,因为它是多对多关系,下面是我的模型: 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, :mo
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
下面是连接模型:
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
如果我运行一个查询来获取一个用户的所有团队以及所有结果团队的用户,如下所示:
teams_users =
from(t in Team, where: t.owner_id == ^user_id)
|> Repo.all()
|> Repo.preload(:team_users)
我得到这个日志:
[%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}]}]
[%App.Team{{uuuuuu meta}:{35; exto.Schema.Metadata,id:1,
基地团队:对,所有者id:3,
团队用户:[%App.TeamUser{{uuuuuuu meta}:#exto.Schema.Metadata,
团队:#异位协会。未加载,
小组编号:1,
用户:#exto.Association.NotLoaded,
用户_id:3},
%App.TeamUser{{uuuuuu meta}:#exto.Schema.Metadata,
团队:#异位协会。未加载,
小组编号:1,
用户:#exto.Association.NotLoaded,
用户_id:4},
%App.TeamUser{{uuuuuu meta}:#exto.Schema.Metadata,
团队:#异位协会。未加载,
小组编号:1,
用户:#exto.Association.NotLoaded,
用户_id:5}]}]
在日志中,我得到了id为1的团队,以及id为:(3、4、5)的所有用户
但是为什么我得到了用户:#exto.Association.NotLoaded
?我没有要求以该id加载用户。。那我为什么得到这样的警告呢
我正在使用
{:phoenix\u-exto,“~>3.0-rc}
您需要预加载:user
以及:team\u-users
:
teams_users =
from(t in Team, where: t.owner_id == ^user_id)
|> Repo.all()
|> Repo.preload(team_users: :user)
文档中有一个关于嵌套关联的部分