ruby-on-rails - 分组错误 : ERROR: column "" must appear in the GROUP BY clause or be used in an aggregate function

标签 ruby-on-rails ruby postgresql heroku group-by

我正在尝试创建一个已发表评论的唯一患者列表。代码字很好,直到我上传到 heroku,它在 postgresql 中不起作用。

这是我创建列表的 Ruby .erb 代码:

<% @comments.group(:patient_id).each_with_index do |comment, index| %>
    <div class="profile-post color-one">
       <span class="profile-post-numb"><%= index+1 %></span>
        <div class="profile-post-in">
            <h3 class="heading-xs"><a><%= link_to "#{comment.patient.first_name} #{comment.patient.last_name}", comment_path(comment) %></a></h3>
            <p>Lastest comment from <%= time_ago_in_words(comment.created_at) %> ago<i class="pull-right"><%= link_to "Edit", edit_comment_path(comment) %> <%= link_to "Delete", comment_path(comment), method: :delete, data: {confirm: 'Are you sure you want to delete'} %></i></p>
        </div>
    </div>
<% end %>

@comments 在 Controller 中定义为:

def index
  @comments = current_clinician.comments.order("created_at desc")
end

heroku 日志给我这个错误信息:

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

LINE 1: SELECT "comments".* FROM "comments"  WHERE  comments"."clini...
               ^
SELECT "comments".* *FROM "comments"  WHERE "comments"."clinician_id" = $1 GROUP BY patient_id  ORDER BY created_at desc

我尝试了其他 SO 问题的解决方案,例如 20942477 .这表示我应该将字段 comments.id 添加到我的组子句中:

<% @comments.group(:patient_id, :"comments.id").each_with_index do |comment, index| %>

这消除了 heroku 上的错误,但违背了 group 命令的目的 - 它不再仅显示独特的患者,而是列出所有患者。

我还尝试了 1780893 中的解决方案.其中说我应该更改 ORDER BY:

@comments = current_clinician.comments.order("substring(created_at,1.8) desc")

这在本地给出了这个错误:

SQLite3::SQLException: no such function: substring: SELECT "comments".* FROM "comments" WHERE "comments"."clinician_id" = ? ORDER BY substring(created_at,1.8) desc

我意识到这是一个常见问题,我对 SQL 没有经验,所以我在获取代码时遇到了困难,因此它无法在我的开发和生产环境中运行。我在 SO 上阅读的答案并没有使用 Ruby 获取 SQL 并超出了我的经验水平。

最佳答案

您不能在 Postgres 中将 SELECT *GROUP BY some_column 结合使用,因为这是矛盾的(除非它从单个表中选择并且 some_column 是它的PK)。所有非聚合列(在聚合函数外的 SELECTHAVINGORDER BY 子句中使用)必须在 GROUP 中BY 列表 - 其中主键列覆盖表的所有列。否则,从聚合集中选取哪个值将是不确定的。

The manual:

When GROUP BY is present, or any aggregate functions are present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions or when the ungrouped column is functionally dependent on the grouped columns, since there would otherwise be more than one possible value to return for an ungrouped column. A functional dependency exists if the grouped columns (or a subset thereof) are the primary key of the table containing the ungrouped column.

已知某些其他 RDBMS 在这里玩肮脏的把戏并允许这样做并选择任意值...

您似乎想要一个已发表评论的独特患者列表,每个患者都有最新评论。 Postgres 中最简单的方法是使用 DISTINCT ON:

SELECT DISTINCT ON (patient_id) *
FROM   comments
WHERE  clinician_id = $1
ORDER  BY patient_id, created_at DESC NULLS LAST;

但这不会与 SQLite 一起运行 - 它不应该在循环中开始。见:

NULLS LAST 仅在 created_at 可以为 NULL 时才相关:

DISTINCT ON 的详细信息:

关于ruby-on-rails - 分组错误 : ERROR: column "" must appear in the GROUP BY clause or be used in an aggregate function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660396/

相关文章:

django - 我可以轻松地覆盖 Django ORM 'iexact' 以使用 LOWER() 而不是 UPPER() 吗?

ruby-on-rails - 使用火箭裤时如何配置JSON响应?

ruby - 在 Ruby 中,您首选的 ODM 是什么? MongoMapper、MongoID 还是 MongoDoc?

ruby-on-rails - Capistrano:不知道如何构建任务 'deploy:new_release_path'

ruby - YARV 数组的 push/pop 方法是线程安全的吗?

mysql - 选择另一个 RDBMS

java - 运行后如何使用用户名、密码和 url 连接到 postgres docker 镜像?

ruby-on-rails - Ruby:将按钮文本更改为图标

ruby-on-rails - Ruby on Rails - Controller 子目录

mysql - Rails has_many 时将 String 类型转换为十进制进行排序