Framework: [5.3] Eloquent model throws " Non-grouping field is used in HAVING clause" error after moving to 5.3

Created on 20 Aug 2016  路  1Comment  路  Source: laravel/framework

This error came up after moving the whole source to the 5.3 version

So I have this kind of eloquent query:

POI::select('*', DB::raw("SQRT( POW((x - {$this->x}),2) + POW((y - {$this->y}),2) ) AS distance"))
        ->where('status', Config::get('app.poi_state.enabled'))
        ->whereNotIn('id', $excludePOIList)
        ->having('distance', '<=', $distance)
        ->orderBy('distance')->get();

It worked find before upgrade now it throws:

Syntax error or access violation: 1463 Non-grouping field 'distance'
is used in HAVING clause (SQL: select *, SQRT( POW((x - 860.0000),2) + POW((y - 105.0000),2) ) AS distance from poi where status = 1 and id not in (1) having distance <= 6 order by distance asc)

I wanted to check if the ONLY_FULL_GROUP_BY mode is enabled on my server, but it isn't...

SELECT @@sql_mode
NO_ENGINE_SUBSTITUTION

The same query works fine in MySQL workbench.
I think there's a bug somewhere

Most helpful comment

Okay, turns out it's not a bug at all, the strict mode is now enabled by default in config/database.php

All I had to do was to override the strict modes by doing the following:

        'strict' => true,
        'engine' => null,
        'modes' => [
            //'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'
        ]

>All comments

Okay, turns out it's not a bug at all, the strict mode is now enabled by default in config/database.php

All I had to do was to override the strict modes by doing the following:

        'strict' => true,
        'engine' => null,
        'modes' => [
            //'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'
        ]
Was this page helpful?
0 / 5 - 0 ratings