我的选择查询会返回一个不同的产品,以及产品所具有的类别列表。但是,它只返回传递给我的查询的^categories
列表中的类别,即使它实际上有另外5个类别。我想返回所有产品类别,即使^categories
列表中只有一个类别传入函数:
def create_query_no_keyword(categories, shop_ids) do
products_shops_categories = from p in Product,
distinct: p.id,
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,
where: c.id in ^categories,
where: s.id in ^shop_ids,
group_by: [p.id, p.name, p.brand],
select: %{product: p, categories: fragment("json_agg( DISTINCT (?, ?)) AS category", c.id, c.name), shops: fragment("json_agg( DISTINCT (?, ?, ?)) AS shop", s.id, s.name, s.point)}
end
如果只有一个类别传入上述函数,当产品实际上有8个类别时,为什么返回的产品只返回一个类别?
架构(product_categories
实际使用Subcategory
- 只是命名不同):
schema "products" do
field :name, :string
field :brand, :string
field :description, :string
field :image, :string
field :rating, :integer
field :number_of_votes, :integer
end
schema "subcategories" do
field :name, :string
end
schema "product_categories" do
field :c_id, :integer
field :p_id, :integer
end
schema "shops" do
field :name, :string
field :place_id, :string
field :point, Geo.Point
field :geo_json, :string, virtual: true
field :distance, :float, virtual: true
timestamps()
end
schema "product_shops" do
field :s_id, :integer
field :p_id, :integer
end