Ecto和Postgres,数据库查询返回带有键f1,f2,f3等的值

时间:2018-06-02 03:14:17

标签: postgresql elixir ecto elixir-framework

我有一个从数据库中选择唯一商店及其产品的功能,但结果会返回f1f2等键。我可以指定要返回的密钥并使它们更有意义吗?

调用数据库代码的代码:

unique_shops = create_unique_shop_query_no_keyword(categories, shop_ids) |> Api.Repo.all
IO.inspect(unique_shops, label: "unique shops!") 

数据库代码:

  def create_unique_shop_query_no_keyword(categories, shop_ids) do
    products_shops_categories = from p in Product,
    join: ps in ProductShop, on: p.id == ps.p_id,
    join: s in Shop, on: s.id == ps.s_id,
    join: pc in ProductCategory, on: p.id == pc.p_id,
    join: c in Subcategory, on: c.id == pc.c_id,
    distinct: s.id,
    where: c.id in ^categories,
    where: s.id in ^shop_ids,
    group_by: [s.id, s.name],
    select: %{products: fragment(
      "json_agg( DISTINCT (?, ?, ?, ?, ?, ?, ?, ?, ?)) AS products", 
      p.id, 
      p.name, 
      p.brand, 
      p.description, 
      p.image, 
      p.rating, 
      p.number_of_votes, 
      ps.not_in_shop_count, 
      ps.price), 
      shop: fragment(
        "json_agg( DISTINCT (?, ?, ST_X(?), ST_Y(?))) AS shop", 
        s.id, 
        s.name, 
        s.point, 
        s.point
        )
      }
  end

这是目前返回的内容:

unique shops!: [
  %{
    products: [
      %{
        "f1" => 96,
        "f2" => "reverb",
        "f3" => "verve",
        "f4" => "we’ve ever revere",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 34
      },
      %{
        "f1" => 97,
        "f2" => "clear we",
        "f3" => "wfwegwaeg",
        "f4" => "vervaerververv",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 444
      },
      %{
        "f1" => 98,
        "f2" => "sdvsd",
        "f3" => "few",
        "f4" => "svsdvsdvsdvsdv",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 3
      },
      %{
        "f1" => 99,
        "f2" => "sdvsdd",
        "f3" => "fewer",
        "f4" => "svsdvsdvsdvsdv",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 3
      },
      %{
        "f1" => 100,
        "f2" => "badd",
        "f3" => "vegd",
        "f4" => "sdvsdvsdvsd",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 33
      },
      %{
        "f1" => 101,
        "f2" => "veer was",
        "f3" => "be",
        "f4" => "rverververvrqv",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 444
      },
      %{
        "f1" => 102,
        "f2" => "erverve",
        "f3" => "every",
        "f4" => "rverververveqrv",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 44
      },
      %{
        "f1" => 104,
        "f2" => "eve",
        "f3" => "g",
        "f4" => "fdvrervqergregeqrg",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => nil
      },
      %{
        "f1" => 105,
        "f2" => "v",
        "f3" => "be",
        "f4" => "verververbebrerbe",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => nil
      },
      %{
        "f1" => 106,
        "f2" => "B.B.",
        "f3" => "cweveve",
        "f4" => "ewvrvreqveqrvervqerv",
        "f5" => "https://vepo-images.s3.amazonaws.com/product-images%2FIMG_0001.JPG",
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 3
      },
      %{
        "f1" => 107,
        "f2" => "be",
        "f3" => "vrvvrv",
        "f4" => "vrvrvrvrvrvrv",
        "f5" => "https://vepo-images.s3.amazonaws.com/product-images%2FIMG_0005.JPG",
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 4
      },
      %{
        "f1" => 108,
        "f2" => "besvwv",
        "f3" => "vrvvrv",
        "f4" => "vrvrvrvrvrvrv",
        "f5" => "https://vepo-images.s3.amazonaws.com/product-images%2FIMG_0005.JPG",
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 4
      }
    ],
    shop: [
      %{
        "f1" => 30,
        "f2" => "Ryoko's Japanese Restaurant & Bar",
        "f3" => -122.4119976,
        "f4" => 37.788211
      }
    ]
  },
  %{
    products: [
      %{
        "f1" => 103,
        "f2" => "vearvr",
        "f3" => "fewer",
        "f4" => "varevaeaver",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => nil
      },
      %{
        "f1" => 109,
        "f2" => "we’vewdvcwce",
        "f3" => "few saves",
        "f4" => "cwecwecwecwecwecwevw",
        "f5" => nil,
        "f6" => nil,
        "f7" => nil,
        "f8" => 0,
        "f9" => 33233
      }
    ],
    shop: [
      %{
        "f1" => 53,
        "f2" => "Red Dog Restaurant & Bar",
        "f3" => -122.395457,
        "f4" => 37.784809
      }
    ]
  }
]

如何指定从数据库返回的值的键名?

我尝试了一些事情,例如:

  shop: fragment(
    "json_agg( DISTINCT json_build_object(?, 'id', ?,'so',  ST_X(?), 'ff',ST_Y(?),'dd')) AS shop", 
    s.id, 
    s.name, 
    s.point, 
    s.point
    )
  }

来自this SO answer以及来自here的一些内容但是知道如何在片段内写入它有点困难。

我的postgres版本是10.4

0 个答案:

没有答案