Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 如何在不预加载关联的情况下加载关联?_Elixir_Ecto - Fatal编程技术网

Elixir 如何在不预加载关联的情况下加载关联?

Elixir 如何在不预加载关联的情况下加载关联?,elixir,ecto,Elixir,Ecto,我有以下两种型号: schema "users" do field :email, :string field :crypted_password, :string field :password, :string, virtual: true has_many :exercise_entries, ExerciseEntry timestamps end 以及: 我在iex中运行了以下代码: u = Repo.get(User, 1) iex(8)

我有以下两种型号:

schema "users" do
    field :email, :string
    field :crypted_password, :string
    field :password, :string, virtual: true

    has_many :exercise_entries, ExerciseEntry

    timestamps
end
以及:

我在iex中运行了以下代码:

u = Repo.get(User, 1)
iex(8)> u.exercise_entries
#Ecto.Association.NotLoaded<association :exercise_entries is not loaded>
u=Repo.get(用户,1)
iex(8)>美国演习项目
#未加载

我知道我可以做
Repo.get(User,1)|>Repo.preload([:exercise\u entries])
然后我可以访问exercise\u条目。不过,是否有可能在不预先加载练习项的情况下以某种方式加载练习项?

Ecto需要执行另一个数据库查询以获取练习项。这将是正常的
Repo.all
query,但您可以使用
exto.assoc

ee = Repo.all(Ecto.assoc(u, :exercise_entries))
ee = Repo.all(Ecto.assoc(u, :exercise_entries))