Problem/Motivation
Adding only a field with a COUNT aggregation on nid, adds a second nid without a GROUP BY clause resulting in this
SQLSTATE[42000]: Syntax error or access violation: 1140 In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'db.node_field_data.nid'; this is incompatible with sql_mode=only_full_group_by: SELECT "node_field_data"."nid" AS "nid_1", COUNT(node_field_data.nid) AS "nid" FROM "node_field_data" "node_field_data" WHERE ("node_field_data"."status" = :db_condition_placeholder_0) AND ("node_field_data"."type" IN (:db_condition_placeholder_1)); Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => course_section )
This is a regression from D7 views as this would produce the COUNT only field.
Steps to reproduce
- Create a new node view on article bundle
- Remove sort clause that gets added and title field
- Turn on aggregation
- Add COUNT on nid field
Proposed resolution
Avoid adding the nid via getEntityTableInfo() when it's not needed, or add the appropriate GROUP By to the field it adds
@see
\Drupal\views\Plugin\views\query\entity_information
core/modules/views/src/Plugin/views/query/Sql.php:1344
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3247121
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
joelpittetJust a note, marking this as "Distinct" will fix the problem as a kind of workaround because at the beginning of query() it adds a groupby on nid_1 that it adds and further down, it checks for $groupby and adds min on to what is now 2 additional nid fields + a count(nid)
public/core/modules/views/src/Plugin/views/query/Sql.php:1278
The problem seems to be that
$groupby
is empty leading to not adding the 'min' function to the new field.public/core/modules/views/src/Plugin/views/query/Sql.php:1337
Comment #3
joelpittetComment #4
jplana CreditAttribution: jplana commentedComment #6
muaz91Having the same problem with user id
Comment #7
johnnny83 CreditAttribution: johnnny83 commentedI get the same error when I aggregate on Content:Type with COUNT.
Comment #8
kazah CreditAttribution: kazah commentedSame problem with webform sumbission fields...
Comment #10
junaidpvFacing same issue. But we cannot apply workaround mentioned in #2 as we just want one row aggregating all results.
Comment #12
ramil g CreditAttribution: ramil g at The University of British Columbia commentedComment #14
ramil g CreditAttribution: ramil g at The University of British Columbia commentedHere's a merge request that fixes it. Let's see if the testbot agrees.
Comment #18
joelpittetWith red/green tests I’ll mark this RTBC. This is better and simpler than the proposed changes I had in mind.
Thanks @ramil g
Comment #19
LendudeReviewed it too, looks good, nice to see the minimal view for the test, makes it nice and clear what is being tested with it.
RTBC +1
Comment #20
alexpottCommitted and pushed bc8ed7fec1 to 10.1.x and f9e87b99e6 to 10.0.x and dde89e2238 to 9.5.x. Thanks!
Will backport to 9.4.x once the branch is not in a freeze.
Added some documentation to the test.
Comment #25
alexpott