子查询上的Ecto.QueryError

时间:2017-02-04 20:33:15

标签: elixir ecto

对于以下查询:

sub_query = from sq in Database.Nestoria,
  distinct: sq.lister_url,
  select: [sq.place_name,
          sq.price,
          sq.price_type],
  where: fragment("cast(to_char(?, 'YYYYMMDD') AS INTEGER) >= (SELECT cast(to_char(max(inserted_at), 'YYYYMMDD') AS INTEGER) - 1 FROM nestoria)", sq.inserted_at),
  where: sq.bedroom_number == 1

query = from un in Database.Underground,
  left_join: ne in subquery(sub_query), on: un.station_name_slug == ne.place_name,
  select: [ un.lines_id,
            un.station_name,
            un.display_name,
            ne.place_name,
            fragment("count(?) as data_count", ne.place_name),
            fragment("Avg(CASE WHEN ? = 'weekly' THEN price * ( 31 / 7 ) ELSE price END) AS avg_monthly_price ", ne.price_type)],
  group_by: [un.lines_id,
            un.station_name,
            un.display_name,
            ne.place_name],
  order_by: [ne.place_name]

output = Repo.all(query)

我收到以下错误:

** (Ecto.QueryError) subquery must select a source (t), a field (t.field) or a map, got: [&0.place_name(), &0.price(), &0.price_type()]

奇怪的是,我已经在一个非常类似的项目中运行而没有遇到麻烦。所有模式都正常工作,没有任何问题(Repo.all(Nestoria / Underground)返回正常)。

1 个答案:

答案 0 :(得分:3)

截至当前版本的Ecto子查询中,不能有Angular 2列表作为值。即使以某种方式工作,您也无法在问题中的查询中select,因为ne.place_name将是列表,而不是地图。

您可以手动选择地图:

ne

或使用select: %{place_name: sq.place_name, price: sq.price, ...} map

struct

或只传递一个字段列表,相当于上面的select: map(sq, [:place_name, :price, :price_type]) # or select: struct(sq, [:place_name, :price, :price_type]) 版本:

struct