File tree Expand file tree Collapse file tree
tests/system/Database/Live Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -472,8 +472,8 @@ public function first()
472472 }
473473
474474 // Some databases, like PostgreSQL, need order
475- // information to consistently return correct results.
476- if (empty ($ builder ->QBOrderBy ) && ! empty ($ this ->primaryKey ))
475+ // information to consistently return correct results when there is group by
476+ if (! empty ( $ this -> QBGroupBy ) && empty ($ builder ->QBOrderBy ) && ! empty ($ this ->primaryKey ))
477477 {
478478 $ builder ->orderBy ($ this ->table . '. ' . $ this ->primaryKey , 'asc ' );
479479 }
Original file line number Diff line number Diff line change @@ -251,6 +251,31 @@ public function testFirst()
251251
252252 //--------------------------------------------------------------------
253253
254+ public function testFirstWithoutGroupBy ()
255+ {
256+ $ model = new UserModel ();
257+
258+ $ user = $ model ->select ('SUM(id) as total ' )
259+ ->where ('id > ' , 2 )
260+ ->first ();
261+ $ this ->assertEquals (7 , $ user ->total );
262+ }
263+
264+ //--------------------------------------------------------------------
265+
266+ public function testFirstWithGroupBy ()
267+ {
268+ $ model = new UserModel ();
269+
270+ $ user = $ model ->select ('SUM(id) as total ' )
271+ ->where ('id > ' , 2 )
272+ ->groupBy ('id ' )
273+ ->first ();
274+ $ this ->assertEquals (3 , $ user ->total );
275+ }
276+
277+ //--------------------------------------------------------------------
278+
254279 public function testFirstRespectsSoftDeletes ()
255280 {
256281 $ this ->db ->table ('user ' )
You can’t perform that action at this time.
0 commit comments