在Ecto片段中使用表别名/查询表达式?

时间:2017-06-28 01:55:23

标签: elixir ecto postgrex

从此查询开始作为Ecto版本的基础:

 select folder_id, json_agg(p.*) 
 from folder_memberships inner join profiles p 
 on p.id=folder_memberships.profile_id 
 where folder_id in (1234) group by folder_id;

我有这段代码:

 # ids=[1234] 
 from(p in Profile,
   join: link in FolderMembership, on: link.profile_id == p.id,
   select: [link.folder_id, fragment("json_agg(?) as members", p)],
   group_by: link.folder_id,
   where: link.folder_id in ^ids
 ) 
 |> Repo.all

这让我得到以下错误:

== Compilation error on file lib/profile.ex ==
** (Ecto.Query.CompileError) variable `p` is not a valid query expression. 
Variables need to be explicitly interpolated in queries with ^
(ecto) expanding macro: Ecto.Query.select/3

我确定我缺少基础知识,但如果我知道它是什么,我就会被解雇。我尝试了很多选项,但我能够看到的所有示例都是fragment("json_agg(?)", p.some_field),而不是p本身。

1 个答案:

答案 0 :(得分:1)

解决方案并不完美,因为它需要明确列出所有字段,并且不允许您从生成的JSON中排除字段。

 # ids=[1234] 
 from(p in Profile,
   join: link in FolderMembership, on: link.profile_id == p.id,
   select: [link.folder_id, fragment("json_agg((?, ?, ?)::profiles) as members", p.id, p.name, p.created_at)],
   group_by: link.folder_id,
   where: link.folder_id in ^ids
 ) 
 |> Repo.all

json_agg中的问号数应与配置文件表中的列数完全相同,并且表中列的顺序应与fragment参数的顺序相对应。我不知道你的架构,所以我做了#34; 3栏 - 我希望你明白这一点。

我在一个简化的例子(没有连接)上尝试了这种方法。我用作游乐场的应用程序的源代码是there

defmodule Magic do
  import Ecto.Query
  alias Badging.{Badge, Repo}

  @fields Badge.__schema__(:fields)
  @source Badge.__schema__(:source)
  @questions Enum.map_join(@fields, ", ", fn _ -> "?" end)
  @json_agg "json_agg((#{@questions})::#{@source})"

  def run do
    fields = Badge.__schema__(:fields)
    source = Badge.__schema__(:source)
    questions = Enum.map_join(fields, ", ", fn _ -> "?" end)
    json_agg = "json_agg((#{questions})::#{source})"

    from(
      b in Badge,
      select: [
        b.id,
        fragment(
          "json_agg((?, ?, ?, ?, ?, ?, ?, ?, ?)::badges)",
          b.id,
          b.identifier,
          b.subject,
          b.status,
          b.color,
          b.svg,
          b.svg_downloaded_at,
          b.inserted_at,
          b.updated_at
        )
      ],
      group_by: b.id
    ) |> Repo.all
  end
end

我还尝试使用Badge.__schema__(:fields)Badge.__schema__(:source)使其更灵活,但偶然发现the inability of fragment to accept variable number of arguments

这是我到目前为止所得到的:

defmodule Magic do
  import Ecto.Query
  alias Badging.{Badge, Repo}

  fields = Badge.__schema__(:fields)
  source = Badge.__schema__(:source)
  questions = Enum.map_join(fields, ", ", fn _ -> "?" end)
  @json_agg "json_agg((#{questions})::#{@source})"

  def run do
    from(
      b in Badge,
      select: [
        b.id,
        fragment(
          @json_agg,
          field(b, :id), # or just b.id
          b.identifier,
          b.subject,
          b.status,
          b.color,
          b.svg,
          b.svg_downloaded_at,
          b.inserted_at,
          b.updated_at
        )
      ],
      group_by: b.id
    ) |> Repo.all
  end
end

我认为技术上可以依赖__schema__(:fields)而不是明确列出所有字段。字段列表在编译时是已知的。我在Elixir / Ecto的宏中做得不好(还)。