searchable icon indicating copy to clipboard operation
searchable copied to clipboard

SQLSTATE[42000]: Syntax error or access violation: 1055 'school.students.name' isn't in GROUP BY

Open gregoriochiko opened this issue 6 years ago • 10 comments

I got error while I'm using this trait normally. Here's my model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;

class Student extends Model
{
    use SearchableTrait;

    protected $searchable = [
        'columns' => [
            'students.name' => 15,
            'students.email' => 10
        ]
    ];
}

And here's my controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Student;

class StudentController extends Controller
{
    public function search(Request $request)  // related to 'students.search' route
    {
        return Student::search($request->query('q'))->get();  // I simplified this function here.
    }
}

And this is how I call the controller from unit test:

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class StudentTest extends TestCase
{
    use RefreshDatabase;

    protected $user;

    public function testSearchStudents()
    {
        $this->actingAs($this->user, 'api')
            ->get(route('students.search', ['q' => 'jack']))
            ->assertJsonStructure([    // I also simplified this section, the point is I compare the given structure exact with what the model should gives
                'id',
                'name'
            ]);
    }

But when I run vendor/bin/phpunit tests/Unit I get this error:

SQLSTATE[42000]: Syntax error or access violation: 1055 'school.students.name' isn't in GROUP BY (SQL: ...

I've tried to remove the column name from the $searchable columns, but it still gives the exactly same error.

gregoriochiko avatar Mar 27 '18 03:03 gregoriochiko

Having the same issue.

$users = User::search('fred', null, true, true)->first();

class User extends Authenticatable { use Notifiable, hasRoles, SearchableTrait;

protected $searchable = [
    /**
     * Columns and their priority in search results.
     * Columns with higher values are more important.
     * Columns with equal values have equal importance.
     *
     * @var array
     */
    'columns' => [
        'users.first_name' => 10,
        'users.last_name' => 10,
        'users.username' => 10,
        'users.qualification' => 10,
        'users.email' => 10
    ]
];

}

lperry65 avatar Mar 29 '18 13:03 lperry65

Temporary solution: set strict mode to false.

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

maxdaniel98 avatar Mar 30 '18 13:03 maxdaniel98

Is there a "structural" solution?

jozeflambrecht avatar Oct 05 '18 13:10 jozeflambrecht

Same error here 😢

gpressutto5 avatar Oct 10 '18 04:10 gpressutto5

Temporary solution: set strict mode to false.

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

this fixed the problem, is it safe to use tho?

SaphiLC avatar Feb 26 '19 00:02 SaphiLC

Anyone update about this error?

ssheduardo avatar Mar 27 '19 14:03 ssheduardo

getting same error , using laravel 5.8

shanks25 avatar May 06 '19 11:05 shanks25

Temporary solution: set strict mode to false.

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

this fixed the problem, is it safe to use tho?

According to this post its ok to set strict mode te false with some conditions.

https://stackoverflow.com/questions/42104412/what-is-the-use-of-strict-in-laravel-config-database

functioneelwit avatar Jun 12 '19 08:06 functioneelwit

It's not wise to disable the strict mode. Never trust yourself.

gpressutto5 avatar Jun 12 '19 12:06 gpressutto5

@gpressutto5: I guess you have a point. Maybe it's a little more secure to only allow grouping by one column like so:

'mysql' => [
       ...
       ....
       'strict' => true,
       'modes' => [
            //'ONLY_FULL_GROUP_BY', // Disable this to allow grouping by one column
            'STRICT_TRANS_TABLES',
            'NO_ZERO_IN_DATE',
            'NO_ZERO_DATE',
            'ERROR_FOR_DIVISION_BY_ZERO',
            'NO_AUTO_CREATE_USER',
            'NO_ENGINE_SUBSTITUTION'
        ],
 ]

Source: https://stackoverflow.com/questions/40917189/laravel-syntax-error-or-access-violation-1055-error

functioneelwit avatar Jun 12 '19 13:06 functioneelwit