X Tutup
The Wayback Machine - https://web.archive.org/web/20201203182559/https://github.com/octobercms/october/issues/5134
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation count on list view with mysql strict_mode enabled results in an error #5134

Open
Klaasie opened this issue Jun 22, 2020 · 3 comments
Open

Comments

@Klaasie
Copy link
Contributor

@Klaasie Klaasie commented Jun 22, 2020

  • OctoberCMS Build: 466
  • PHP Version: 7.2^
  • Database Engine: MySQL MariaDB 10.3.23 (with strict set to true through database.php configuration)

Description:

For one of our plugins we're currently running into the error described below:

SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

The query in question:

SELECT   `model_a`.*, 
         ( 
                  SELECT   count(*) 
                  FROM     `model_b` 
                  WHERE    `model_a`.`id` = `model_b`.`model_a_id`
                  ORDER BY `sort_order` ASC) AS `model_b_count`, 
         `model_a`.* 
FROM     `model_a` 
ORDER BY `NAME` DESC limit 20 offset 0;

The issue is the result of a combination using count on a model with the sortable trait (which will add sorting through a global scope) and the mysql strict setting being true.

I've traced the issue back to the withCount method in src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php. So it's an issue in Laravel.
Digging further I've noticed that this issue has actually been resolved in 7.x with this pull request: https://github.com/laravel/framework/pull/29925/files
In 6.x however this has not been fixed.

Additionally: withCount seems to add a model.* to the query, the list widget adds another model.* to the select at the end of the method. It's a bit weird but this does not cause any issues.

Steps To Reproduce:

Apply 'strict' => true, to the mysql database configuration through database.php.

Create a plugin with 2 models and a controller with list view.
Model 1 has many Model 2.
Model 2 has sortable trait.
Model 1 columns shows count of model 2 relations.
Visit the list view page and see an error.

@LukeTowers
Copy link
Member

@LukeTowers LukeTowers commented Jun 22, 2020

@Klaasie feel free to port the linked PR to the October Rain library as a PR (including the tests if possible).

@github-actions
Copy link

@github-actions github-actions bot commented Aug 22, 2020

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@github-actions
Copy link

@github-actions github-actions bot commented Oct 22, 2020

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.
X Tutup