Rails 应用程序中的 Heroku PostgreSQL GROUP_BY 错误 [英] Heroku PostgreSQL GROUP_BY error in Rails app

查看:25
本文介绍了Rails 应用程序中的 Heroku PostgreSQL GROUP_BY 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在开发中运行良好的 rails 应用程序 (SQLite),但是当我通过 Heroku 部署它时抛出了很多错误,Heroku 使用了我收集的 PostgreSQL.

I've got a rails app that works fine in development (SQLite) but is throwing lots of errors when I've deployed it via Heroku, which uses PostgreSQL I gather.

返回的错误信息:

        ActionView::Template::Error (PGError: ERROR:  
column "practices.id" must appear in the GROUP BY clause or be used in an aggregate function: 
SELECT "practices".* 
FROM "practices" WHERE ("practices".activity_id = 1) 
AND ("practices"."created_at" BETWEEN '2011-01-01' AND '2011-01-31') 
GROUP BY DATE(created_at) ORDER BY created_at DESC):

当我调用以下代码时会抛出这个:

this is being thrown when I call the following:

def month_days_not_practiced(date = Date.today)
   p = practices.where(:created_at => date.at_beginning_of_month..date.at_end_of_month).group("DATE(created_at)").to_a.count
   days_in_month(date.year, date.month) - p
end

我真的很想保持代码干净,这样它就可以在开发和生产数据库上工作......有人能解释一下吗?

I'd really like to keep the code clean so it works on both development and production DBs...can anyone shed some light?

我已经试过了:

def month_days_not_practiced(date = Date.today)
   p = practices.where(:created_at => date.at_beginning_of_month..date.at_end_of_month).group("practices.id, DATE(created_at)").to_a.count
   days_in_month(date.year, date.month) - p
end

无济于事...

tia.

推荐答案

您需要对选择列表中的所有列进行分组,所以

You need to group by all the columns in the select list, so

.group("practices.id, practices.foo, practices.bar, ..., DATE(created_at)")

您仅按 id 分组的想法是合理的(假设 id 是主键),但 PostgreSQL 仅在 9.1 版中支持.

Your idea to group only by id is sound (assuming id is the primary key), but PostgreSQL will only support that in version 9.1.

这篇关于Rails 应用程序中的 Heroku PostgreSQL GROUP_BY 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆