是否可以在ecto查询的预加载中进行选择?

问题描述 投票:0回答:1

我需要从Post模式中选择具有10多个字段的特定字段。我还需要选择预加载的:comments。我该怎么办?

query = from p in Post, preload: [:comments], select: map(p, [:comments, :title])
Repo.all(query)
elixir ecto
1个回答
1
投票

有多种方法可以执行此操作,具体取决于您希望接收哪种数据结构作为输出。如果要只包含%Post{}comments字段的title结构,请使用以下查询:

query = from p in Post, preload: [:comments], select: [p.comments, p.title]
Repo.all(query)

否则,请参考文档以获取select查询表达式:-)

© www.soinside.com 2019 - 2024. All rights reserved.