1

我正在研究 Rails 5.2 项目并尝试ActiveRecord::Relation按表中的字段进行分组:

rails 控制台中的以下内容返回错误:

2.4.0 :015 > Post.group(:published)
  Post Load (3.9ms)  SELECT  `posts`.* FROM `posts` GROUP BY `posts`.`published` LIMIT 11
ActiveRecord::StatementInvalid: Mysql2::Error: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'app_dev.posts.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by: SELECT  `posts`.* FROM `posts` GROUP BY `posts`.`published` LIMIT 11

但是,添加.sum()正确运行没有错误......

> Post.group(:published).sum(:views)

是否有明显的原因第一个查询会失败,而不是第二个?

在 schema.rb

create_table "post", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
  t.string "title", null: false
  t.text "body", null: false
  t.boolean "published", null: false, default: false
  t.bigint "views", null: false, default: 0
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

谢谢

4

1 回答 1

0

第一种情况的问题是您在only_full_group_by模式下运行 mysql,这意味着您的 select 语句中的列不能不依赖于group by 子句中的列:

MySQL 5.7.5 及更高版本实现了功能依赖检测。如果启用了 ONLY_FULL_GROUP_BY SQL 模式(默认情况下),MySQL 拒绝选择列表、HAVING 条件或 ORDER BY 列表引用非聚合列的查询,这些列既不在 GROUP BY 子句中命名,也不在功能上依赖于它们.

如果您查看 Rails 生成的第二个查询,您会看到 sum 方法将聚合函数添加到选择列表中,SUM(views)并将其他属性限制为仅在 group 方法中的一个,结果如下:

SELECT SUM(post.views) AS sum_views, post.published AS post_published FROM post GROUP BY post.published

如果您使用:

Post.select("SUM(views), published").group(:published).as_json
于 2020-01-24T13:38:59.530 回答