Fix comment failing test

This is the error I'm seeing [in the latest commit](5338e5a4b3)

Seems to be related to a MySQL flag `sql_mode=only_full_group_by` enabled by default.

```ruby
(byebug) Comment.where(user: user).group(:thread_id).order("id desc").limit(20).pluck(:thread_id)
*** ActiveRecord::StatementInvalid Exception: Mysql2::Error:
Expression #1 of ORDER BY clause is not in GROUP BY clause and
contains nonaggregated column 'lobsters_test.comments.id' which is not
functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by

nil
```

I don't think is a good idea disabling that flag, so, I just updated the query.

Old query
```ruby
(byebug) Comment.where(user: user).group(:thread_id).order("id desc").limit(20)

(byebug) Comment.where(user: user).group(:thread_id).order("id desc").limit(20).to_sql
"SELECT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 1 GROUP BY `comments`.`thread_id` ORDER BY id desc LIMIT 20"
```

New query
```ruby
(byebug) Comment.where(user: user).select(:thread_id).order("id desc").limit(20)

(byebug) Comment.where(user: user).select(:thread_id).order("id desc").limit(20).to_sql
"SELECT `comments`.`thread_id` FROM `comments` WHERE `comments`.`user_id` = 1 ORDER BY id desc LIMIT 20"

(byebug) Comment.where(user: user).select(:thread_id).order("id desc").limit(20).pluck(:thread_id)
[1]
```

```mysql
mysql> explain SELECT DISTINCT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 1 ORDER BY `comments`.`thread_id` DESC LIMIT 20;
+----+-------------+----------+------------+------+---------------------------+---------------------------+---------+-------+------+----------+----------------+
| id | select_type | table    | partitions | type | possible_keys             | key                       | key_len | ref   | rows | filtered | Extra          |
+----+-------------+----------+------------+------+---------------------------+---------------------------+---------+-------+------+----------+----------------+
|  1 | SIMPLE      | comments | NULL       | ref  | index_comments_on_user_id | index_comments_on_user_id | 8       | const |    1 |   100.00 | Using filesort |
+----+-------------+----------+------------+------+---------------------------+---------------------------+---------+-------+------+----------+----------------+
1 row in set, 1 warning (0.01 sec)

mysql> explain SELECT comments.* FROM comments WHERE comments.user_id = 1 GROUP BY comments.thread_id ORDER BY comments.thread_id DESC LIMIT 20;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'lobsters_dev.comments.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
```

Links:
  * https://stackoverflow.com/questions/41887460/select-list-is-not-in-group-by-clause-and-contains-nonaggregated-column-inc
  * https://stackoverflow.com/questions/54547279/mysql-contains-nonaggregated-column-error-when-using-group-rails-5-2
  * https://gitlab.com/gitlab-org/gitlab-foss/-/issues/48428
This commit is contained in:
Juan Vasquez 2023-10-03 16:00:45 -06:00
parent 5338e5a4b3
commit 642d888ec8
1 changed files with 2 additions and 2 deletions

View File

@ -601,8 +601,8 @@ class Comment < ApplicationRecord
thread_ids = Comment
.where(user: user)
.group(:thread_id)
.order("id desc")
.distinct(:thread_id)
.order(thread_id: :desc)
.limit(20)
.pluck(:thread_id)
return Comment.none if thread_ids.empty?