Rails 应用程序中的 Heroku PostgreSQL GROUP_BY 错误

发布于 2024-10-10 05:35:58 字数 1115 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2024-10-17 05:35:58
Practice.group(Practice.col_list)



def self.col_list
  Practice.column_names.collect {|c| "practices.#{c}"}.join(",")
end
Practice.group(Practice.col_list)



def self.col_list
  Practice.column_names.collect {|c| "practices.#{c}"}.join(",")
end
彼岸花似海 2024-10-17 05:35:58

您需要按选择列表中的所有列进行分组,因此

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

仅按id分组的想法是合理的(假设id是主要的key),但 PostgreSQL 仅在 9.1 版本中支持该功能。

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

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

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.

白日梦 2024-10-17 05:35:58
Practice.joins(:foobar).group_all

def self.group_all
  group(group_all_columns)
end

def self.group_all_columns
  @group_all_columns ||= column_names.collect {|c| "#{table_name}.#{c}"}.join(",")
end
Practice.joins(:foobar).group_all

def self.group_all
  group(group_all_columns)
end

def self.group_all_columns
  @group_all_columns ||= column_names.collect {|c| "#{table_name}.#{c}"}.join(",")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文