0
歡迎.....(導軌)Postgres的錯誤(列 「posts.id」 必須出現在GROUP BY子句)
我想在我的Rails應用程序對用戶最後
@section.posts.select("posts.*, count(posts.user_id) as user_posts_count").group('posts.user_id')
.order("user_posts_count DESC").limit(10)
但我有問題PostgreSQL的,而我沒有任何問題的代碼工作的sqlit3和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...
我需要使用PostgreSQL,因爲它是最適合的Heroku 但我不知道爲什麼我得到這個錯誤上PostgreSQL的
我希望如果有人幫我>>>
永遠不要使用'選擇*'與'組'通過'。 –
這個錯誤告訴你到底什麼是錯的。你的'select()'中的每一列也必須在你的'group()'中。當然不包括'count()','sum()','avg()'等等。您可能只想選擇類似user_id的東西。 「@ section.posts.select(」user_id,count(user_id)as user_posts_count「)。group(:user_id).order(」user_posts_count DESC「)。limit(10)'。 – Dan
感謝兄弟現在的工作,我想我還是需要更多的經驗再次感謝你,但是爲什麼要用mysql和sqlite工作而在postgreSQL上不工作 –