Read more

MySQL 5.7.5 enables `ONLY_FULL_GROUP_BY` mode per default

Natalie Zeumann
June 22, 2017Software engineer at makandra GmbH

When using GROUP BY, MySQL now complains if the SELECT includes columns which are not part of the GROUP BY.

Reason:

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

There could be multiple values for those columns per group but only one value can be picked for the results.

The default behaviour of MySQL prior to version 5.7 will not complain and arbitrarily choose a value. But this leads to non-deterministic results. So MySQL now has enabled the only_full_group_by setting by default to prevent this.

In Rails this could lead to some trouble, because scopes do not have specific select columns in the most cases. A simple id can cause the error if the group method is used in a scope.

Solutions:

  1. make your queries comply with it, if possible

  2. change the sql mode in the config/database.yml (and that of all your colleagues, staging and production servers!):

development:
  adapter: mysql2
  ...
  variables:
    sql_mode: TRADITIONAL

(Be aware, that this solution disables the only_full_group_by setting globally and MySQL will select those values arbitrarily again)

Also see our card on SQL compatibility modes.

  1. Install MySQL 5.6 in parallel, using mysql-sandbox

  2. Use MariaDB 10.x

What TRADITIONAL changes

MySQL 5.7 standard TRADITIONAL
STRICT_TRANS_TABLES STRICT_TRANS_TABLES
- STRICT_ALL_TABLES
NO_ZERO_IN_DATE NO_ZERO_IN_DATE
NO_ZERO_DATE NO_ZERO_DATE
ERROR_FOR_DIVISION_BY_ZERO ERROR_FOR_DIVISION_BY_ZERO
NO_AUTO_CREATE_USER NO_AUTO_CREATE_USER
NO_ENGINE_SUBSTITUTION NO_ENGINE_SUBSTITUTION
ONLY_FULL_GROUP_BY -
Natalie Zeumann
June 22, 2017Software engineer at makandra GmbH
Posted by Natalie Zeumann to makandra dev (2017-06-22 10:26)