- Düzenlendi
Merhaba,
İlla ki çoğu kişinin başına geliyordur. MySQL'in only_full_group_by sql modu ile ilgili bir sorun.
Şimdi laravel üzerinden değil de direkt laravel'den aldığım SQL sorgusu üzerinden gidelim.
Products ve skus tablolarım var bu ikisi arasında ilişki kuruyorum.
color_id gibi "null" olabilen bir sütunum da var.
Şimdi bir ürünün 3 farklı rengi ve birden fazla bedeni de olabileceği için skus tablosunda o ürünün adını yazdığımda tüm renkleri ve ona bağlı bedenleri ile ilgili 8 tane ürün gelebiliyor. Bende color_id ile groupBy yapıp sayıyı düşürmek istiyorum. Örnek kod;
Laravel sorgusu;
Sku::with('product.category')
->whereHas('product', function ($q) {
$q->where('name', 'LIKE', '%'.$this->query.'%');
})
->groupBy('product_id', 'color_id');
SQL Çıktısı;
select * from skus where exists (select * from products where skus.product_id = products.id and name LIKE ? and products.deleted_at is null) group by product_id, color_id
Ama strict mode true ise Laravel hatası şu şekildedir.
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'exampleDb.skus.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from
skus
where exists (select * fromproducts
whereskus
.product_id
=products
.id
andname
LIKE %baby% andproducts
.deleted_at
is null) group byproduct_id
,color_id
)
MySQL hatası ise;
SQL Error (1055): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'naturkorb.skus.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
1- Burada neden bunu yapmamıza aslında izin vermiyor?
2- Zorla bunu yaptırırsak ayrıca bir hataya yol mu açmış oluruz? (normalde her şey istediğim gibi çalışıyor)