【发布时间】:2018-07-11 10:42:41
【问题描述】:
有没有办法通过选择另一个连接的列来预加载记录?
# table structure
# User 1---* Post 1---* PostTag *---1 Tag
# extract definition of scheme
scheme "posts" do
...
has_many :post_tags, PostTag
has_many :tags, [:post_tags, :tag]
end
以下伪代码表达了我的目标(但不起作用)。
query = from post in Post,
join: user in User, on post.user_id == user.id,
select: %{
id: post.id,
title: post.title,
user_name: user.name, # <= column at joined table
},
preload: [:tags]
Repo.all(query)
#=> ** (Ecto.QueryError) the binding used in `from` must be selected in `select` when using `preload` in query:`
我期待这样的结果。
[
%{id: 1, title: "AAA", user_name: "John", tags: [%{name: "elixir"},...]},
%{id: 2, title: "BBB", user_name: "Mike", tags: [%{name: "erlang"},...]},
...
]
【问题讨论】:
标签: elixir phoenix-framework ecto