英文:
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
有多种方法可以做到这一点,具体取决于您希望将什么样的数据结构作为输出。如果您想要一个仅包含 comments
和 title
字段的 %Post{}
结构体,请使用以下查询:
query = from p in Post, preload: [:comments], select:
Repo.all(query)
否则,请参考 select 查询表达式的文档说明
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论