Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable only_full_group_by in MySQL or Sequelize

In Sequelize, how would I do the equivalent of SET sql_mode='' to avoid getting the following error?

SequelizeDatabaseError: ER_WRONG_FIELD_WITH_GROUP: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'assets.group_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Or, alternatively, how do I change the default configuration of MySQL such that the sql_mode is always ''?

Thanks!

like image 624
lwang135 Avatar asked Mar 09 '16 04:03

lwang135


People also ask

What is MySQL Only_full_group_by?

If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.

How do I get rid of only full group by?

To disable ONLY_FULL_GROUP_BY in MySQL, remove ONLY_FULL_GROUP_BY from the sql-mode setting in the MySQL configuration file, /etc/my. cnf . This file can only be modified via SSH as the root user or WHM >> Terminal. ONLY_FULL_GROUP_BY cannot be disabled for a single account or database.

How do I change sql mode in MySQL?

To change the SQL mode at runtime, set the global or session sql_mode system variable using a SET statement: SET GLOBAL sql_mode = 'modes'; SET SESSION sql_mode = 'modes'; Setting the GLOBAL variable requires the SUPER privilege and affects the operation of all clients that connect from that time on.


2 Answers

Get the existing sql_mode and remove only the ONLY_FULL_GROUP_BY value rather than making it completely empty.

SELECT @@sql_mode; -- Get the current sql_mode

You might get a result as follows;

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Now remove the ONLY_FULL_GROUP_BY from the result and update the sql_mode

SET GLOBAL sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
SET SESSION sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";

Now you are done.

Note that SET GLOBAL is to update the global setting which will not take effect until you restart the mysql server or service. SET SESSION will immediately take this effect on your current session even without restarting mysql server. Therefore, you can use either one or both depending on your requirement.

Alternatively, instead of disabling this setting, from MySQL 5.7 onwards, you can simply modify the query by using ANY_VALUE function on whichever the field is throwing error. For example ANY_VALUE(assets.group_id) as mentioned in the error message you have posted.

like image 68
Firnas Avatar answered Oct 15 '22 13:10

Firnas


Try this:

SET GLOBAL sql_mode = '';
like image 24
Dylan Su Avatar answered Oct 15 '22 14:10

Dylan Su