This question already has an answer here:
GroupingError: ERROR: column “ ” must appear in the GROUP BY clause or be used in an aggregate function
(1个答案)
两年前关闭。
我有汽车和汽车城市模型。我想买至少有一辆车的城市。
协会;
Cars
belongs_to :car_city

CarCities
has_many :cars

连接查询;
CarCity.joins(:cars).group("cars.car_city_id").having("count(cars.id) > 0")

这对我当地的环境很管用。但不在生产上工作。
错误;
An ActionView::Template::Error occurred in search#search_city:

PG::GroupingError: ERROR:  column "car_cities.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT “car_cities".* FROM "car_cities” INNER JOIN "cars"…

^
: SELECT “car_cities".* FROM “car_cities” INNER JOIN “cars” ON “cars"."car_city_id” = “car_cities"."id” WHERE "car_cities"."car_country_id” = $1 GROUP BY cars.car_city_id HAVING count(cars.id) > 0

app/views/search/_search_filters.html.erb:88:in `block in _app_views_search__search_filters_html_erb__3215729544581234245_70286113854020'

最佳答案

如果可能,请在生产控制台上尝试此操作。。

CarCity.joins(:cars).group("car_cities.id").having("count(cars.id) > 0")

关于ruby-on-rails - 滑轨| PG::GroupingError ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45138459/

10-12 23:01