How to solve the MySQL 5.7.30 "sql_mode=only_full_group_by" problem
I did a fresh install of Homestead which includes MySQL 5.7.30. I brought across all my Laravel files from my Linux system which also runs MySQL 5.7.30.
For some reason bizaar reason when I run queries on the Homestead system I get the problem:
Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'contraxiq.orders.quote_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Ok - so inside the Homestead version I set the config/database mysql strictsettings to "false". I have also tried turning off the error with no effect.
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
I have dozens of queries with groupby. As an example, here is one which generates the error:
public function open_orders() {
return DB::table('orders')
->join('customers', 'customers.id', '=', 'orders.customer_id')
->select('orders.reference', 'quote_name','start_date', 'company_name', 'customers.firstname1', 'customers.lastname1', 'customers.city', 'orders.total', 'orders.created_at')
->where('orders.status', '=', 'open')
->groupBy('orders.reference')
->orderBy('orders.created_at')
->get();
}
Wat do I do to fox this problem ? I do not want to rewrite all the dozens of queries as suggested by this post: https://gabi.dev/2016/03/03/group-by-are-you-sure-you-know-it/
Many Thanks !!
Selecting custom modes will override "strict" mode.
These are the default modes Laravel sets on strict mode:
MySQL 8:
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
Other MySQL versions:
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
Try setting it to that according to your version (and exclude ONLY_FULL_GROUP_BY).
Please or to participate in this conversation.