with-join icon indicating copy to clipboard operation
with-join copied to clipboard

Don't add selecting of all fields if not needed

Open lespavl opened this issue 9 years ago • 3 comments

I'm using Postgresql and have the following code:

$items = Element::select(DB::raw("COUNT(1), category"))->groupBy('category')->get();

Mysql processes this query without any problems, but in case of Postgresql I'm getting an error:

column "id" must appear in the GROUP BY clause or be used in an aggregate function

That's because your package adds extra select elements.* and Postgresql works in such way, that I need to specify, what to do with each selected field.

lespavl avatar Apr 24 '15 13:04 lespavl

Same problem seems to be adding table.* on the query even though you already have table.* on your select. It is in sql server. On Mysql it is doing fine

crissi avatar Apr 30 '15 12:04 crissi

I've just found out that this will also be an issue when doing union queries This package is selecting all columns on all select statements in union queries, making the column numbers do not agree if we're union-ing different tables with different column numbers.

Exception thrown:

SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns

Example of generated query:

(select A, B, table1.* from table1) # table1 has 3 columns
union
(select A, B, table2.* from table2) # table2 has 4 columns

Temporary workaround is to use Laravel's query builder (DB class) instead of table's model when doing unions.

indratjhai avatar Dec 03 '15 18:12 indratjhai

+1

xxxcoltxxx avatar Mar 03 '16 23:03 xxxcoltxxx