Postgresql 希望返回所有类别的产品,但只返回传递到查询中的产品
我的select查询返回一个不同的产品,以及该产品具有的类别列表。但是,它只返回传递到我的查询中的Postgresql 希望返回所有类别的产品,但只返回传递到查询中的产品,postgresql,ecto,elixir,Postgresql,Ecto,Elixir,我的select查询返回一个不同的产品,以及该产品具有的类别列表。但是,它只返回传递到我的查询中的^categories列表中的类别,即使它实际上还有5个类别。我想返回所有产品类别,即使传递给函数的^categories列表中只有一个类别: def create_query_no_keyword(categories, shop_ids) do products_shops_categories = from p in Product, distinct: p.id,
^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个类别时,如果只有一个类别传递到上述函数中,为什么返回的产品只返回一个类别
模式(产品类别
实际上使用了子类别
——只是名称不同):
因为
其中:^categories中的c.id
。你不使用预加载有什么原因吗?@JustinWood我想这意味着它只会选择列表中有类别的产品^categories
,但会返回整个产品及其类别。你能发布你的模式吗,至少是相关的位,也许我们可以告诉你需要做什么才能得到你想要的信息?你有什么理由不使用预加载吗?@JustinWood没有理由,我只是做了我认为应该做的事。我是外星生物的新手。预加载是否适用于我的模式?
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