简体   繁体   English

laravel是否会自动添加分组依据? 奇怪的行为Laravel toSql与正在运行的查询。

[英]Does laravel automatically add group by's? Weird behaviour Laravel toSql vs query being run.

I have a very, VERY weird issue. 我有一个非常非常奇怪的问题。 Our team has ignored this for a while because it hasn't broken anything (I know, not a good attitude), but I really want to know what the hell is/could be going on. 我们的团队已经忽略了一段时间,因为它没有破坏任何东西(我知道,态度不好),但是我真的很想知道到底是怎么回事。 We are on Laravel 5.2. 我们使用Laravel 5.2。

We have built very long queries using the laravel eloquent query builder. 我们使用laravel雄辩的查询构建器构建了非常长的查询。 For some reason, sometimes, when we do a toSql(), we'll get the query we meant to run. 出于某种原因,有时,当我们执行toSql()时,我们会得到要运行的查询。 When pasting the query, I'll get an error like this one: 粘贴查询时,我将收到如下错误:

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'onion.distance' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Weird part though, changing nothing about it, laravel will run the query just fine! 尽管很奇怪,但对此保持不变,laravel可以很好地运行查询! Anyone have ANY idea why laravel would run the query fine, but the toSql out put trigger this error? 任何人都有任何想法,为什么laravel可以很好地运行查询,但是toSql out发出会触发此错误? And how could we avoid it (other than manually adding the Group By's). 以及如何避免这种情况(除了手动添加分组依据之外)。

Not pasting code unless requested (lots of moving parts to some of the queries). 除非有要求,否则不要粘贴代码(一些查询中有很多活动部件)。

Thank you to Marcin Nabialek, you were 100% right. 谢谢Marcin Nabialek,您100%正确。 Laravel was overriding the default mysql server configuration and setting strict mode to false. Laravel覆盖了默认的mysql服务器配置,并将strict模式设置为false。 For anyone that was confused as I was: 对于像我一样感到困惑的任何人:

database.php database.php

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'dump_command_timeout' => 60 * 15, // 5 minute timeout
    'unix_socket' => env('UNIX_SOCKET', ''),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', 'mysqltest'),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
]

All I had to do was set strict to true 我要做的就是严格遵守

'strict' => true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM