Elixir/Phoenix 查询错误 (grouping_error)

Elixir/Phoenix query ERROR (grouping_error)

我有以下查询:

fooditems = Repo.all(from p in OrderItem, join: f in Food,
                                          on: p.food_id == f.id,
                                          where: p.inserted_at >= ^date_from and p.inserted_at <= ^date_to,
                                          group_by: f.name,
                                          select: %{name: f.name, quantity: sum(p.quantity)},
                                          order_by: [desc: f.category_id])

OrderItem table 有 food_id 列,而 Food table 有 namecategory_id。我需要结果按 Food table 的 category_id 排序。但是,查询似乎不起作用。它正在返回:

ERROR (grouping_error): column "f1.category_id" must appear in the GROUP BY clause or be used in an aggregate function

但是,我不想按类别对它进行分组,只是为了以一种将来自相同 category_id 的项目一起显示的方式来呈现它 (order_by)。 如何进行查询以按 category_id 列出项目?谢谢

按照@HarrisonLucas 的建议,将我的查询更改为以下内容:

fooditems = Repo.all(from p in OrderItem, full_join: f in Food,
                                          on: p.food_id == f.id,
                                          where: p.inserted_at >= ^date_from and p.inserted_at <= ^date_to,
                                          group_by: f.name,
                                          group_by: f.category_id,
                                          select: %{name: f.name, quantity: sum(p.quantity)},
                                          order_by: [asc: f.category_id])

现在看来可以了。谢谢!