How to disable only_full_group_by option in Laravel

大城市里の小女人 提交于 2020-01-03 16:47:39

问题


I am new to laravel and I am having an issue with DB problem.

I have disabled 'only_full_group_by' sql_mode by editing /etc/mysql/my.cnf file. And I checked sql_mode for both global and session using SELECT @@GLOBAL.sql_mode; and SELECT @@SESSION.sql_mode; and confirmed that sql_mode no longer has only_full_group_by.

However, when I make a request through postman, it gives me the error saying this is incompatible with sql_mode=only_full_group_by.

I am so confused. Why do I get this error even after I changed sql_mode? Am I doing something wrong?

Any suggestion or advice would be appreciated.

Thank you.

SQL using toSql()

select A.* 
from `A` 
inner join `B` on `A`.`id` = `B`.`a_id` 
inner join `C` on `C`.`id` = `B`.`c_id` 
group by `A`.`id` having COUNT(A.id) > 0;

回答1:


This is the wrong way of going about this. Rather than turning off only_full_group_by mode, you should be fixing your query so that it doesn't break MySQL:

SELECT a1.*
FROM A a1
INNER JOIN
(
    SELECT A.id
    FROM A
    INNER JOIN B
        ON A.id = B.a_id 
    INNER JOIN C
        ON C.id = B.c_id
    GROUP BY A.id
    HAVING COUNT(A.id) > 0
) a2
    ON a1.id = a2.id;

I don't know why your attempts to turn off strict mode failed, but you should not be relying on it in any case.




回答2:


To answer the original question, if you want to disable this in Laravel you have to change the 'strict' value to false in your database configuration in config/database.php.

'connections' => [
...

    'mysql' => [
    ...
        'strict' => false,
        ...

    ],

]



回答3:


Add this modes to config/database.php

'mysql' => [
 ...
        'modes' => [
            'STRICT_ALL_TABLES',
        ],
],

If you set 'strict' => true. Some sql exception will not throw like sql data integrity exception.



来源:https://stackoverflow.com/questions/48145384/how-to-disable-only-full-group-by-option-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!