I run a CakePHP 2.4.7 application, using PostgreSQL as Database.
It seems that CakePHP or the PGSQL DBO source
don't correctly format the SQL queries for Postgre when contain Group By
or Order BY
.
I gotten this error trying to save a "simple" model acting as Tree via TreeBehavior. It's from a POST / create action.
Database Thrown Error
Error: SQLSTATE[42803]: Grouping error: 7 ERROR: column "Emplacement.reference" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...RE 1 = 1 AND "Emplacement"."id" <> 126 ORDER BY "reference... ^
SQL Query:
SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement"
WHERE 1 = 1 AND "Emplacement"."id" <> 126 ORDER BY "reference" ASC LIMIT 1
Stack Trace:
CORE/Cake/Model/Datasource/DboSource.php line 458 → PDOStatement->execute(array)
CORE/Cake/Model/Datasource/DboSource.php line 424 → DboSource->_execute(string, array)
CORE/Cake/Model/Datasource/DboSource.php line 665 → DboSource->execute(string, array, array)
CORE/Cake/Model/Datasource/DboSource.php line 1077 → DboSource->fetchAll(string, boolean)
CORE/Cake/Model/Model.php line 2903 → DboSource->read(Emplacement, array)
CORE/Cake/Model/Model.php line 2875 → Model->_readDataSource(string, array)
CORE/Cake/Model/Behavior/TreeBehavior.php line 1013 → Model->find(string, array)
CORE/Cake/Model/Behavior/TreeBehavior.php line 932 → TreeBehavior->_getMax(Emplacement, string, string, integer, boolean)
CORE/Cake/Model/Behavior/TreeBehavior.php line 96 → TreeBehavior->_setParent(Emplacement, string, boolean)
[internal function] → TreeBehavior->afterSave(Emplacement, boolean, array)
CORE/Cake/Utility/ObjectCollection.php line 132 → call_user_func_array(array, array)
[internal function] → ObjectCollection->trigger(CakeEvent)
CORE/Cake/Event/CakeEventManager.php line 247 → call_user_func(array, CakeEvent)
CORE/Cake/Model/Model.php line 1861 → CakeEventManager->dispatch(CakeEvent)
APP/Controller/EmplacementsController.php line 87 → Model->save(array)
[internal function] → EmplacementsController->admin_add()
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(EmplacementsController, array)
CORE/Cake/Routing/Dispatcher.php line 185 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 160 → Dispatcher->_invoke(EmplacementsController, CakeRequest, CakeResponse)
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Since the query is written in the Core Framework that I don't know, I request your help to understand and resolve this issue.
Do you think that upgrading my version to cakephp-2.6 could be help ?
Postgre SQL (regular SQL specifications) reject this request (made by cake in my case):
SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement"
WHERE 1 = 1 AND "Emplacement"."id" = 126
ORDER BY "reference" ASC LIMIT 1
but it seems that mySQL is Okay with that.
PostgreSQL accepted queries could be :
SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement"
WHERE 1 = 1 AND "Emplacement"."id" = 126
or
SELECT MAX("Emplacement"."rght") AS "rght"
FROM "mpdefault"."emplacements" AS "Emplacement"
WHERE 1 = 1 AND "Emplacement"."id" = 126
group by "reference"
ORDER BY "reference" ASC LIMIT 1
But these queries are non-sense.
Why CakePHP add a "ORDER BY" or "ORDER BY" clause on a MAX()
SELECT ?