Rails 应用程序中的 Heroku PostgreSQL GROUP_BY 错误
我有一个 Rails 应用程序,在开发中运行良好(SQLite),但当我通过 Heroku 部署它时,它会抛出很多错误,它使用我收集的 PostgreSQL。
返回错误消息:
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):
当我调用以下命令时会抛出此错误:
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
我真的很想保持代码干净,以便它适用于开发和生产数据库...任何人都可以透露一些信息吗?
我已经尝试过:
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
无济于事......
蒂亚。
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.
the error message getting returned:
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?
I have tried this:
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
to no avail...
tia.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要按选择列表中的所有列进行分组,因此
仅按
id
分组的想法是合理的(假设id
是主要的key),但 PostgreSQL 仅在 9.1 版本中支持该功能。You need to group by all the columns in the select list, so
Your idea to group only by
id
is sound (assumingid
is the primary key), but PostgreSQL will only support that in version 9.1.