Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG::GroupingError: ERROR: column "events.id" must appear in the GROUP BY clause or be used in an aggregate function

An event has a column popularity and many keywords. A keyword has a category and a name. I am trying to order events by their popularity, but then only return the most popular event from each keyword name with the category "taxonomy".

Here's my query:

Event
  .order(:popularity)
  .joins(:keywords)
  .where(keywords: {category: "taxonomy"})
  .group("keywords.name")

But I am getting below error:

PG::GroupingError: ERROR: column "events.id" must appear in the GROUP BY clause or be used in an aggregate function

Where am I going wrong?

like image 772
Jackson Cunningham Avatar asked Oct 18 '16 19:10

Jackson Cunningham


1 Answers

Event
  .order(:popularity)
  .joins(:keywords)
  .group('events.id') # <======
  .where(keywords: { category: 'taxonomy' })
  .group('keywords.name')
like image 159
Andrey Deineko Avatar answered Oct 09 '22 23:10

Andrey Deineko