Rails postgreSQL GroupingError

时间:2015-02-14 22:33:57

标签: ruby-on-rails sqlite postgresql activerecord heroku

所以我正在努力解决看似常见的问题。我用SQLite开发了我的rails应用程序,现在将它推送到Heroku(postgreSQL)并遇到了这个错误:

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

由于我的代码:

@projects = @user.projects.select('MAX(id), *').group(:unique_id).all.paginate(page: params[:page])

我将所有user project拉出来。 unique_id标识每个项目,我使用MAX(id)作为拉动项目最新版本的方式。

如何更改我的代码以使其可用于开发的SQLite和生产的postgreSQL?我已经查看了其他几个处理这个错误的问题,但是找不到我正在做的很好的解决方案。我倾向于使用distinct代替group,但我不确定如何使用{{1}}。提前谢谢。

2 个答案:

答案 0 :(得分:0)

我认为你的问题是" *"在select方法中。如果用" unique_id"替换它,则应该满足SQL解析器。

@projects = @user.projects.select('MAX(id), unique_id').group(:unique_id).all.paginate(page: params[:page])

答案 1 :(得分:0)

看起来像这样。选择max(id),unique_id,other_column1,other_column2等......然后,您必须按其他列进行分组。