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

huangapple go评论62阅读模式
英文:

Is it possible to do select on preload in ecto query?

问题

我需要从 Post 模式中选择具有 10+ 个字段的特定字段。
我还需要选择预加载的 :comments。我该如何做到这一点?

查询 = 从 p in Post,预加载:[:comments],选择:map(p,[:comments,:title])
Repo.all(query)

英文:

I need to select specific fields from the Post schema which have 10+ fields.
I need to select preloaded :comments as well. How can I do that?

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

答案1

得分: 2

有多种方法可以做到这一点,具体取决于您希望将什么样的数据结构作为输出。如果您想要一个仅包含 commentstitle 字段的 %Post{} 结构体,请使用以下查询:

query = from p in Post, preload: [:comments], select: 

Repo.all(query)

否则,请参考 select 查询表达式的文档说明 在Ecto查询中是否可以对预加载进行选择?

英文:

There are multiple ways of doing this, depending on what kind of data structure you want to receive as output. If you want a %Post{} struct with only the comments and title fields, use the following query:

query = from p in Post, preload: [:comments], select: 

Repo.all(query)

Otherwise, please refer to the documentation for the select query expression 在Ecto查询中是否可以对预加载进行选择?

huangapple
  • 本文由 发表于 2020年1月3日 23:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定