Mohammad Khojaly
Mohammad Khojaly

Reputation: 63

(rails) postgres error (column "posts.id" must appear in the GROUP BY clause)

Welcome.....

I want to get the most user post on my rails app

@section.posts.select("posts.*, count(posts.user_id) as user_posts_count").group('posts.user_id')
.order("user_posts_count DESC").limit(10)

But I have issue with postgreSQL while my code work without any issue on sqlit3 and mysql2

PG::GroupingError: ERROR: column "posts.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT posts.*, count(posts.user_id) as user_posts_count FR...

I need to use postgreSQL because it is the best for heroku but i don't know why I get this error on PostgreSQL

I hope if anyone help me >>>

Upvotes: 0

Views: 775

Answers (1)

maxgomes
maxgomes

Reputation: 329

remove the id from the group, you should add all others fields in group by, using .* is a bad pratice, pls don't use .* in select that have for example, a count()

Upvotes: 1

Related Questions