The Taxonomy and User Representative Node (GroupwiseMax) using a RepresentativeView does not work because the existing code generates SQLSTATE[42000]: Syntax error or access violation and SQLSTATE[HY093]: Invalid parameter number: no parameters were bound.

To reproduce:

  1. Install Drupal 8.
  2. Edit the "People" View.
  3. Add a Representative Node Relationship.
  4. Select content View as the Representative view.
  5. Click Apply (All Displays).
  6. You'll see the error below in the Preview section

Result:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES' at line 3: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {users_field_data} users_field_data LEFT JOIN {node_field_data} node_field_data_users_field_data ON (SELECT node_field_dataINNER.nid AS nidINNER FROM {node_field_data} node_field_dataINNER INNER JOIN {users_field_data} users_field_data_node_field_dataINNER ON "node_field_dataINNER".uid = "users_field_data_node_field_dataINNER".uid LEFT JOIN {users_field_data} users_field_data_node_field_data_1INNER ON "node_field_dataINNER".uid = "users_field_data_node_field_data_1INNER".uid WHERE (("users_field_data_node_field_data_1INNER".uid = users_field_data.uid )) AND (("node_field_dataINNER".status = "1 OR (node_field_dataINNER".uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES*** = 1) OR ***BYPASS_NODE_ACCESS*** = 1)) ORDER BY node_field_dataINNER.changed DESC LIMIT 1 OFFSET 0) = node_field_data_users_field_data.nid WHERE (users_field_data.default_langcode = :db_condition_placeholder_0) AND (users_field_data.uid != :db_condition_placeholder_1)) subquery; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 0 )

Apply this patch, clear the cache, and refresh the View edit page to see it work. Add a node if you want to see it get data.

Tests are needed for this and it seems GroupwiseMax in general (without the Representative View.) Maybe additional discussion is needed? Maybe there is a better way to replace the **CORRELATED** placeholder? Maybe tests should be a different issue?

This issue also exists in Views for Drupal 7. I created the patch for D7 Views over a year ago here: https://www.drupal.org/node/1417090 #35 with a recent update in #53. The patch in #35 has ~10 RTBC's.

CommentFileSizeAuthor
#66 2379423-66.patch5.69 KBneclimdul
#64 2379423-64.patch5.7 KBneclimdul
#61 Before patch.png1.01 MBnikhilraut
#61 After Patch.png2.49 MBnikhilraut
#58 interdiff_55-58.txt616 bytespooja saraah
#58 2379423-58.patch1.1 KBpooja saraah
#55 drupal-2379423.patch1.02 KBlandure
#50 2379423-50.patch5.99 KBneclimdul
#50 2379423-50.interdiff.txt1.98 KBneclimdul
#44 Screenshot 2020-06-04 at 4.57.23 PM.png229.52 KBsharma.amitt16
#40 views-representative_entity-2379423-40.patch6.62 KBnarendra.rajwar27
#37 views-representative_entity-2379423-37.patch5.1 KBnarendra.rajwar27
#35 views-representative_entity-2379423-35.patch1.1 KBnarendra.rajwar27
#25 interdiff-2379423-21-25.txt1.89 KBckaotik
#25 views-representative_entity-2379423-25.patch4.96 KBckaotik
#24 interdiff-2379423-21-24.txt2.04 KBckaotik
#24 views-representative_entity-2379423-24.patch5.11 KBckaotik
#21 representative-view-2379423-21.patch3.42 KBGaëlG
#9 representative-view-2379423-9.patch3.46 KBAnonymous (not verified)
#1 representative-view-2379423-1.patch3.42 KB13rac1

Issue fork drupal-2379423

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

13rac1’s picture

13rac1’s picture

Issue summary: View changes
xjm’s picture

Issue tags: +VDC

Thanks @eosrei for the D8 patch!

xjm’s picture

dawehner’s picture

mradcliffe’s picture

The query fails on PostgreSQL because ON statement table aliases aren't escaped so __INNER becomes "inner". This is going to be very difficult to fix because condition is a string so it would be very complex to create any sort of string replacement. I don't like the idea of doing that anywhere - in the driver or in the views module. Ideally condition should not be a string anymore, but I think it is way too late for a major API change from Drupal 7.

I looked at simply not using a case-sensitive alias in this case, but I was still getting an invalid FROM error in PostgreSQL for the following query.

I'm kind of at an impasse here. I don't think there's any good code that can fix the issue with join conditions without a significant API change, but there really is no other way to fix the issue.

SELECT COUNT(*) AS expression
	FROM 
	(SELECT 1 AS expression
	FROM 
	taxonomy_term_data taxonomy_term_data
	LEFT JOIN node node_taxonomy_term_data ON (SELECT nodeinner.nid AS nidinner
	FROM 
	node nodeinner
	LEFT JOIN taxonomy_index taxonomy_indexinner ON nodeinner.nid = taxonomy_indexinner.nid
	LEFT JOIN taxonomy_term_data taxonomy_term_data_nodeinner ON taxonomy_indexinner.tid = taxonomy_term_data_nodeinner.tid
	INNER JOIN taxonomy_term_field_data taxonomy_term_data_node__taxonomy_term_field_datainner ON taxonomy_term_data_nodeinner.tid = taxonomy_term_data_node__taxonomy_term_field_datainner.tid
	WHERE (( (taxonomy_term_data_node__taxonomy_term_field_datainner.tid = taxonomy_term_field_data.tid ) ))
	ORDER BY nodeinner.nid DESC
	LIMIT 1 OFFSET 0) = node_taxonomy_term_data.nid
	INNER JOIN taxonomy_term_field_data taxonomy_term_field_data ON taxonomy_term_data.tid = taxonomy_term_field_data.tid) subquery

ERROR:  invalid reference to FROM-clause entry for table "taxonomy_term_field_data"
LINE 12: ...rm_data_node__taxonomy_term_field_datainner.tid = taxonomy_t...
                                                              ^
HINT:  Perhaps you meant to reference the table alias "taxonomy_term_data_node__taxonomy_term_field_datainner".
jaredsmith’s picture

I looked at simply not using a case-sensitive alias in this case, but I was still getting an invalid FROM error in PostgreSQL for the following query.

Right -- to get rid of that error, you have to do the JOIN on the taxonomy_term_field_data before referencing that table in the subquery. In other words, do the INNER JOIN on taxonomy_term_field_data before the LEFT JOIN on node. Unfortunately, I have no idea where to look to switch the order of those JOINs.

mgifford’s picture

Status: Needs review » Needs work

Needs re-roll.

Anonymous’s picture

mgifford’s picture

Status: Needs work » Needs review

Go bots...

hctom’s picture

Status: Needs review » Needs work

I just gave this patch a try, but it unfortunately does not solve my issues, because I have a slightly different view setup. My term is a relationship as well, wich is then the relationship for the representative node.

This results in the following error message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'taxonomy_term_field_data.tid' in 'where clause'.

So the field name is generated as taxonomy_term_field_data.tid, but it has to be taxonomy_term_field_data_node_field_data.tid because of the second relationship.

Steps to reproduce

  • Create a node view
  • Add a "Taxonomy terms on node" relationship (term_node_tid)
  • Add the "Representative node" relationship (tid_representative) using the other relationship
  • Run the view preview and you will get the "Unknown column..." error

So I guess there are some escaping issues not using $this->tableAlias where necessary, as this variable already has the correct base table alias.

EDIT:

Forgot to mention that, if i replace the wrong field name, my query works completely fine - with and without your patch.

hctom’s picture

... and just another edit: Your patch definitely solves other SQL issues than mine ;) I had to extend the GroupwiseMax relationship plugin to add some more filters to the temporary view used for querying the represantitive node. Without your patch, my filters received values from the parent view, but with you patch everything works as expected for that case. So the only thing that should be fixed is the one mentioned above, that the field name is generated wrong.

dawehner’s picture

It is great that people actually care about this!

Just a really quick review, sorry for no feedback in such a long time.

  1. +++ b/core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php
    @@ -272,10 +268,10 @@ class GroupwiseMax extends RelationshipPluginBase {
    -      // But if we're using a whole view, we don't know what we have!
    -      if ($options['subquery_view']) {
    

    Why do we ignore the option now?

  2. +++ b/core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php
    @@ -283,17 +279,31 @@ class GroupwiseMax extends RelationshipPluginBase {
    +    views_query_views_alter($subquery);
    ...
    +    // Replace subquery argument placeholders.
    +    $quoted = $subquery->getArguments();
    +    $connection = \Drupal\Core\Database\Database::getConnection();
    +    foreach ($quoted as $key => $val) {
    +      // Replace the **CORRELATED** placeholder with the outer field name.
    +      if ($val === '**CORRELATED**') {
    +       $quoted[$key] = $this->definition['outer field'];
    +      }
    +      // Add quotes for all other values
    +      else {
    +        $quoted[$key] = $connection->quote($val);
    +      }
    +    }
    ...
    +    $subquery_sql = strtr($subquery_sql, $quoted);
    

    I'm wondering whether we could replace **CORRELATED** in views_query_views_alter()

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

x.meglio@gmail.com’s picture

As of today, is there any work-around for the issue? I'm experiencing it in 8.4 for a simple case: a view of taxonomy terms, with one representative item per term. SQL fails.

The only filter is the vocabulary type. The only relationship is Representative node.

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '".deleted = :views_join_condition_0 WHERE ("taxonomy_term_field_data_node_field_' at line 3:...

Even though the issue is in Drupal core, last comment was 2 years ago. Is it because there is no obvious solution to the problem?

oresh’s picture

Issue summary: View changes

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

GaëlG’s picture

Here's a reroll of #9 for 8.4, so that it can be referenced with composer. It still contains the problems mentioned since #9, but it's better than nothing and fixes the bug we had.

yogeshmpawar’s picture

Status: Needs work » Needs review

Triggering test bots.

pitpap’s picture

Go to Views find the view that causes this problem and copy it. When you are sure that you copied the view, delete the old view and place again the new copy wherever you want in your site.
Worked for me.

ckaotik’s picture

The problem mentioned in #18 is due to conditionNamespace assuming it's getting a single "foo.a = bar.b" condition. But if you're using a field of the node, this will actually be "node_field_dataINNER.nid = foo.entity_id AND node__field_dateINNER.deleted = '0'", which contains an AND that's not handled.

Furthermore, the prefixing assumes that the left part of the comparison is what needs to be namespaced. However, this is not always true, e.g. for LEFT JOIN {taxonomy_index} taxonomy_indexINNER ON "node_field_dataINNER".nid = taxonomy_index.nid. In this case, the namespace is applied to the wrong variable.

This should fix the problem from #11/#18. Regardless, there is still the feedback from #13, tests to be written and the code style isn't quite up to standards either.

ckaotik’s picture

The last submitted patch, 24: views-representative_entity-2379423-24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

GiorgosK’s picture

patch applies with following minor problems

Checking patch core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php...
Hunk #1 succeeded at 193 (offset 1 line).
Hunk #2 succeeded at 243 (offset 1 line).
Hunk #3 succeeded at 269 (offset 1 line).
Hunk #4 succeeded at 280 (offset 1 line).
Hunk #5 succeeded at 339 (offset 1 line).
Applied patch core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php cleanly.

worked for 8.3.7 as expected but does not work for 8.5.1

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

neclimdul’s picture

This plugin is pretty complicated, seems like a hard problem to solve.

  1. +++ b/core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php
    @@ -278,16 +279,31 @@ protected function leftQuery($options) {
    +    $connection = \Drupal\Core\Database\Database::getConnection();
    

    This would generally be \Drupal::database(); or at the least not fully qualified.

    But more importantly can we safely assume this is running on the same database as the default connection? Couldn't the quoting fail to match the database if its running on a different connection?

  2. +++ b/core/modules/views/src/Plugin/views/relationship/GroupwiseMax.php
    @@ -278,16 +279,31 @@ protected function leftQuery($options) {
    +       $quoted[$key] = $this->definition['outer field'];
    

    Indention is off by one and fails standards check.

  3. $subquery_sql = strtr($subquery_sql, $quoted);
    

    string replacing into a query sets off warnings for me but I don't have a better solution. The complexity of what's going on here doesn't offer an obvious solution and is why this issue and that odd todo about the order by exist. At the very least we have to be sure our quoting is 100% right per my earlier comment.

RoSk0’s picture

Patch #25 fixes the problem for me. Thank you very much!

Tested on Drupal 8.3.7, MariaDB 5.5.64.

Taxonomy term view with required "Representative node" relationship without "Representative view".

The error was:

Drupal\Core\Database\DatabaseExceptionWrapper: Exception in Learning levels[learning_levels]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'taxonomy_term_field_data.tid1' in 'where clause': SELECT taxonomy_term_field_data.weight AS taxonomy_term_field_data_weight, taxonomy_term_field_data.tid AS tid, node_field_data_taxonomy_term_field_data.nid AS node_field_data_taxonomy_term_field_data_nid FROM {taxonomy_term_field_data} taxonomy_term_field_data INNER JOIN {node_field_data} node_field_data_taxonomy_term_field_data ON (SELECT node_field_dataINNER.nid AS nidINNER FROM {node_field_data} node_field_dataINNER LEFT JOIN {taxonomy_index} taxonomy_indexINNER ON "node_field_dataINNER".nid = "taxonomy_indexINNER".nid LEFT JOIN {taxonomy_term_field_data} taxonomy_term_field_data_node_field_dataINNER ON "taxonomy_indexINNER".tid = "taxonomy_term_field_data_node_field_dataINNER".tid WHERE ("taxonomy_term_field_data_node_field_dataINNER".tid = taxonomy_term_field_data.tid1 ) ORDER BY node_field_dataINNER.nid DESC LIMIT 1 OFFSET 0) = node_field_data_taxonomy_term_field_data.nid LEFT JOIN {taxonomy_term__field_cc_level} taxonomy_term__field_cc_level ON taxonomy_term_field_data.tid = taxonomy_term__field_cc_level.entity_id AND (taxonomy_term__field_cc_level.deleted = :views_join_condition_0 AND taxonomy_term__field_cc_level.langcode = taxonomy_term_field_data.langcode) WHERE (taxonomy_term_field_data.vid IN (:db_condition_placeholder_2)) AND (taxonomy_term__field_cc_level.field_cc_level_value <> :db_condition_placeholder_3) ORDER BY taxonomy_term_field_data_weight ASC LIMIT 11 OFFSET 0; Array ( [:db_condition_placeholder_2] => learning_level [:db_condition_placeholder_3] => 1 [:views_join_condition_0] => 0 ) in Drupal\views\Plugin\views\query\Sql->execute() (line 1488 of /vagrant/web/core/modules/views/src/Plugin/views/query/Sql.php).

knyshuk.vova’s picture

Status: Needs review » Reviewed & tested by the community

I had the same issue on Drupal 8.8.1. The patch #25 fixes my error. Good job, @ckaotik , thank you.

The error was:

Drupal\Core\Database\DatabaseExceptionWrapper: Exception in Platform[platform_special_topics_taxonomy_editors_pick]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'taxonomy_term_field_data.tid1' in 'where clause': SELECT taxonomy_term__field_teaser_media.field_teaser_media_target_id AS taxonomy_term__field_teaser_media_field_teaser_media_target_, taxonomy_term_field_data.name AS taxonomy_term_field_data_name, taxonomy_term_field_data.description__value AS taxonomy_term_field_data_description__value, node_field_data_taxonomy_term_field_data.created AS node_field_data_taxonomy_term_field_data_created, COUNT(node_field_data_taxonomy_term_field_data.nid) AS node_field_data_taxonomy_term_field_data_nid, MIN(taxonomy_term_field_data.tid) AS tid, MIN(node_field_data_taxonomy_term_field_data.nid) AS node_field_data_taxonomy_term_field_data_nid_1, MIN(field_special_topic_taxonomy_term_field_data.nid) AS field_special_topic_taxonomy_term_field_data_nid FROM {taxonomy_term_field_data} taxonomy_term_field_data INNER JOIN {node_field_data} node_field_data_taxonomy_term_field_data ON (SELECT node_field_dataINNER.nid AS nidINNER FROM {node_field_data} node_field_dataINNER LEFT JOIN {taxonomy_index} taxonomy_indexINNER ON "node_field_dataINNER".nid = "taxonomy_indexINNER".nid LEFT JOIN {taxonomy_term_field_data} taxonomy_term_field_data_node_field_dataINNER ON "taxonomy_indexINNER".tid = "taxonomy_term_field_data_node_field_dataINNER".tid WHERE ("taxonomy_term_field_data_node_field_dataINNER".tid = taxonomy_term_field_data.tid1) ORDER BY node_field_dataINNER.created DESC LIMIT 1 OFFSET 0) = node_field_data_taxonomy_term_field_data.nid INNER JOIN {node__field_special_topic} node__field_special_topic ON taxonomy_term_field_data.tid = node__field_special_topic.field_special_topic_target_id AND node__field_special_topic.deleted = :views_join_condition_0 INNER JOIN {node_field_data} field_special_topic_taxonomy_term_field_data ON node__field_special_topic.entity_id = field_special_topic_taxonomy_term_field_data.nid LEFT JOIN {taxonomy_term__field_teaser_media} taxonomy_term__field_teaser_media ON taxonomy_term_field_data.tid = taxonomy_term__field_teaser_media.entity_id AND taxonomy_term__field_teaser_media.deleted = :views_join_condition_1 WHERE (taxonomy_term_field_data.vid IN (:db_condition_placeholder_2)) AND (node_field_data_taxonomy_term_field_data.status = :db_condition_placeholder_3) AND (field_special_topic_taxonomy_term_field_data.status = :db_condition_placeholder_4) GROUP BY taxonomy_term__field_teaser_media_field_teaser_media_target_, taxonomy_term_field_data_name, taxonomy_term_field_data_description__value, node_field_data_taxonomy_term_field_data_created ORDER BY node_field_data_taxonomy_term_field_data_created DESC LIMIT 4 OFFSET 0; Array ( [:db_condition_placeholder_2] => special_topics [:db_condition_placeholder_3] => 1 [:db_condition_placeholder_4] => 1 [:views_join_condition_0] => 0 [:views_join_condition_1] => 0 ) in Drupal\views\Plugin\views\query\Sql->execute() (line 1543 of core/modules/views/src/Plugin/views/query/Sql.php).
knyshuk.vova’s picture

Version: 8.6.x-dev » 8.8.x-dev
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We still need tests here. The changes are complex and this plugin is obviously under tested.

Given that we have clear steps to reproduce the fail adding a test that fails on HEAD and passes with the patch should be possible.

narendra.rajwar27’s picture

Assigned: Unassigned » narendra.rajwar27
narendra.rajwar27’s picture

Version: 8.8.x-dev » 8.8.5
Status: Needs work » Needs review
FileSize
1.1 KB

Testing the issue with drupal 8.8.5. SInce patch #25 is already in core. But still adding relationship for representative node, got this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INNER. DESC LIMIT 1 OFFSET 0) = node_field_data_users_field_data.nid WHERE (user' at line 4: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {users_field_data} users_field_data LEFT JOIN {node_field_data} node_field_data_users_field_data ON (SELECT node_field_dataINNER.nid AS nidINNER FROM {node_field_data} node_field_dataINNER INNER JOIN {users_field_data} users_field_data_node_field_dataINNER ON "node_field_dataINNER".uid = "users_field_data_node_field_dataINNER".uid LEFT JOIN {users_field_data} users_field_data_node_field_data_1INNER ON "node_field_dataINNER".uid = "users_field_data_node_field_data_1INNER".uid WHERE (("users_field_data_node_field_data_1INNER".uid = users_field_data.uid)) AND (("node_field_dataINNER".status = 1 OR ("node_field_dataINNER".uid = 1 AND 1 <> 0 AND 1 = 1) OR 1 = 1)) ORDER BY INNER. DESC LIMIT 1 OFFSET 0) = node_field_data_users_field_data.nid WHERE (users_field_data.default_langcode = :db_condition_placeholder_0) AND (users_field_data.uid != :db_condition_placeholder_1)) subquery; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 0 )

Here is Issue for sort criteria: right syntax to use near 'INNER. DESC LIMIT 1

While adding relationship for representative node it has field Representative sort criteria. But sort order clause is not providing table and field separately. Its just providing the table_name.field_name. So added a possible workaround here. since tables are aliased by appending subquery_namespace.

neclimdul’s picture

Status: Needs review » Needs work

SInce patch #25 is already in core

Not any version of core I'm aware of.

The call to \Drupal\Core\Database\Database::getConnection(); is still pretty troubling to me.

Also alex's tests comment still hasn't been addressed.

narendra.rajwar27’s picture

neclimdul my bad, it was due to abnormal termination of composer.

Refering the issue in comment #35, I updated the patch file. The Patch file additionally include the changes.

Status: Needs review » Needs work

The last submitted patch, 37: views-representative_entity-2379423-37.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

neclimdul’s picture

Version: 8.8.5 » 9.1.x-dev

no worries, just making sure we get the right patches :) moving target to d9.

narendra.rajwar27’s picture

Assigned: narendra.rajwar27 » Unassigned
Status: Needs review » Fixed
daffie’s picture

Status: Fixed » Needs review
datawench’s picture

Thank you, Narendra, for patch #40. It fixed this issue for me when updating a Drupal 8.7 site to 8.8.5.

It kills me that this issue has to be rerolled for every new version of core.

sharma.amitt16’s picture

Thanks @narendra for your contribution. Patch #40 works for me. I tried to reproduce the issue as per the steps given in IS with latest code of 9.1.x branch. I am able to reproduce the issue.

Before patch applies I am getting below error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES' at line 11: SELECT COUNT(*) AS "expression" FROM (SELECT 1 AS "expression" FROM {users_field_data} "users_field_data" LEFT JOIN {node_field_data} "node_field_data_users_field_data" ON (SELECT "node_field_dataINNER"."nid" AS "nidINNER" FROM {node_field_data} "node_field_dataINNER" INNER JOIN {users_field_data} "users_field_data_node_field_dataINNER" ON "node_field_dataINNER".uid = "users_field_data_node_field_dataINNER".uid LEFT JOIN {users_field_data} "users_field_data_node_field_data_1INNER" ON "node_field_dataINNER".uid = "users_field_data_node_field_data_1INNER".uid WHERE (("users_field_data_node_field_data_1INNER".uid = users_field_data.uid)) AND (("node_field_dataINNER".status = "1 OR (node_field_dataINNER".uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES*** = 1) OR ***BYPASS_NODE_ACCESS*** = 1)) ORDER BY "node_field_dataINNER"."changed" DESC LIMIT 1 OFFSET 0) = node_field_data_users_field_data.nid WHERE ("users_field_data"."default_langcode" = :db_condition_placeholder_0) AND ("users_field_data"."uid" != :db_condition_placeholder_1)) "subquery"; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 0 )

After applying patch it works well and error is resolved.

After patch apply

Good to move to RTBC.

Demma10’s picture

I've installed the patch in comment #40 and am having the same problem that hctom had in comment #11. With the patch the representative node relationship works on views that use taxonomy term as the base table, however the relationship doesn't work for views that use node as the base table (where the taxonomy term itself is brought in via a relationship).

This is the error Views gives:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'taxonomy_term_field_data.tid' in 'where clause': SELECT COUNT(*) AS expression FROM (SELECT DISTINCT node__field_rs_release_date.field_rs_release_date_value AS node__field_rs_release_date_field_rs_release_date_value, node_field_data.nid AS nid, taxonomy_term_field_data_node__field_rs_product_link.tid AS taxonomy_term_field_data_node__field_rs_product_link_tid, node_field_data_taxonomy_term_field_data.nid AS node_field_data_taxonomy_term_field_data_nid, 1 AS expression FROM {node_field_data} node_field_data LEFT JOIN {node__field_rs_product_link} node__field_rs_product_link ON node_field_data.nid = node__field_rs_product_link.entity_id AND node__field_rs_product_link.deleted = :views_join_condition_0 LEFT JOIN {taxonomy_term_field_data} taxonomy_term_field_data_node__field_rs_product_link ON node__field_rs_product_link.field_rs_product_link_target_id = taxonomy_term_field_data_node__field_rs_product_link.tid LEFT JOIN {node_field_data} node_field_data_taxonomy_term_field_data ON (SELECT node_field_dataINNER.nid AS nidINNER FROM {node_field_data} node_field_dataINNER LEFT JOIN {taxonomy_index} taxonomy_indexINNER ON "node_field_dataINNER".nid = "taxonomy_indexINNER".nid LEFT JOIN {taxonomy_term_field_data} taxonomy_term_field_data_node_field_dataINNER ON "taxonomy_indexINNER".tid = "taxonomy_term_field_data_node_field_dataINNER".tid WHERE ("taxonomy_term_field_data_node_field_dataINNER".tid = taxonomy_term_field_data.tid) ORDER BY nidINNER DESC LIMIT 1 OFFSET 0) = node_field_data_taxonomy_term_field_data.nid LEFT JOIN {node__field_rs_release_date} node__field_rs_release_date ON node_field_data.nid = node__field_rs_release_date.entity_id AND node__field_rs_release_date.deleted = :views_join_condition_1 WHERE (node_field_data.status = :db_condition_placeholder_0) AND (node_field_data.type IN (:db_condition_placeholder_1))) subquery; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => release [:views_join_condition_0] => 0 [:views_join_condition_1] => 0 )

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Timmy_Cos’s picture

Status: Needs review » Needs work

Unfortunately there appears to be an issue with the patch in #40 :(

After applying the patch custom sort fields would not apply and ordering was defaulting to node ids.

Looking at the changes these isset operations feel a bit off to me, should they be checking $temp_view->query->fields[$order_key] rather than just $order_key?

+      $sort_table_field = explode('.', $order_key);
+      $sort_table = isset($order_key['table']) ?
+        $temp_view->query->fields[$order_key]['table'] : current($sort_table_field);
+      $sort_field = isset($order_key['field']) ?
+        $temp_view->query->fields[$order_key]['field'] : end($sort_table_field);

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

neclimdul’s picture

conflicted with #3222769: [November 8, 2021] Replace all list (array destructuring) assignment to the array syntax

also fixing the database connection because its still not fixed and now its failing PHPCS.

Also, Timmy is right, that looks really weird. order_key has to be a string for the explode but then we try to check the values like an array? One of those cases where isset is hiding errors i think. I've reverted those changes and all the tests seem happy so lets see.

glynster’s picture

@neclimdul thanks so much for the updated patch, resolves the issue and applies cleanly for Drupal 9.3. RTBC + 1

Nathan Tsai’s picture

Patch #50 also worked for me on 9.3.0. Thanks, @neclimdul !

lucuhb’s picture

Patch #50 doesn't seem to work for me on 9.3.6. I tried to have a list of all terms of a taxonomy (of name serie) order by terms last used in contents (so last used terms should be displayed first) .
Here is the configuration I tried on a taxonomy based view:
- add representative node in relationship, order by a date field (descendant) (field_date_publication)
- add new "Relationship : field_serie : Taxonomy term" with "representative node" relationship selected
- Order criteria : (Representative Node) Content : publication Date (desc), with "representative node" selected in relationship

=> views gives this error :
SQLSTATE[HY000]: General error: 4078 Illegal parameter data types row and int for operation '=': SELECT DISTINCT "taxonomy_term_field_data"."tid" AS "tid", "node_field_data_taxonomy_term_field_data"."nid" AS "node_field_data_taxonomy_term_field_data_nid", "taxonomy_term_field_data_node__field_serie"."tid" AS "taxonomy_term_field_data_node__field_serie_tid", DATE_FORMAT(node_field_data_taxonomy_term_field_data__node__field_date_publication.field_date_publication_value, '%Y%m%d') AS "node_field_data_taxonomy_term_field_data__node__field_date_p", 'liste_series:page_1' AS "view_name" FROM "drupal_taxonomy_term_field_data" "taxonomy_term_field_data" LEFT JOIN "drupal_node_field_data" "node_field_data_taxonomy_term_field_data" ON (SELECT "node_field_dataINNER"."nid" AS "nidINNER", ':default' AS "view_name" FROM "drupal_node_field_data" "node_field_dataINNER" LEFT JOIN "drupal_taxonomy_index" "taxonomy_indexINNER" ON "node_field_dataINNER".nid = "taxonomy_indexINNER".nid LEFT JOIN "drupal_taxonomy_term_field_data" "taxonomy_term_field_data_node_field_dataINNER" ON "taxonomy_indexINNER".tid = "taxonomy_term_field_data_node_field_dataINNER".tid WHERE ("taxonomy_term_field_data_node_field_dataINNER".tid = taxonomy_term_field_data.tid) ORDER BY "node_field_dataINNER"."created" DESC LIMIT 1 OFFSET 0) = node_field_data_taxonomy_term_field_data.nid LEFT JOIN "drupal_node__field_serie" "node_field_data_taxonomy_term_field_data__node__field_serie" ON node_field_data_taxonomy_term_field_data.nid = node_field_data_taxonomy_term_field_data__node__field_serie.entity_id AND node_field_data_taxonomy_term_field_data__node__field_serie.deleted = :views_join_condition_0 LEFT JOIN "drupal_taxonomy_term_field_data" "taxonomy_term_field_data_node__field_serie" ON node_field_data_taxonomy_term_field_data__node__field_serie.field_serie_target_id = taxonomy_term_field_data_node__field_serie.tid LEFT JOIN "drupal_node__field_date_publication" "node_field_data_taxonomy_term_field_data__node__field_date_publication" ON node_field_data_taxonomy_term_field_data.nid = node_field_data_taxonomy_term_field_data__node__field_date_publication.entity_id AND node_field_data_taxonomy_term_field_data__node__field_date_publication.deleted = :views_join_condition_1 AND (node_field_data_taxonomy_term_field_data__node__field_date_publication.langcode = node_field_data_taxonomy_term_field_data.langcode OR node_field_data_taxonomy_term_field_data__node__field_date_publication.bundle = :views_join_condition_3) WHERE ("taxonomy_term_field_data"."vid" IN (:db_condition_placeholder_4)) AND ("taxonomy_term_field_data"."status" = :db_condition_placeholder_5) ORDER BY "node_field_data_taxonomy_term_field_data__node__field_date_p" DESC; Array ( [:db_condition_placeholder_4] => serie [:db_condition_placeholder_5] => 1 [:views_join_condition_0] => 0 [:views_join_condition_1] => 0 [:views_join_condition_3] => article )

I have the same error when I remove the "Relationship : field_serie : Taxonomy term" relationship.

landure’s picture

At the risk of being wrong: the issue for me came from the encasing done by "conditionNamespace" at line 324 of GroupwiseMax.php that is not compatible with MySQL/MariaDB. I've tried with this correction that worked for me:

  /**
   * Helper function to namespace query pieces.
   *
   * Turns 'foo.bar' into '"foo_NAMESPACE".bar'.
   * PostgreSQL doesn't support mixed-cased identifiers unless quoted, so we
   * need to quote each single part to prevent from query exceptions.
   */
  protected function conditionNamespace($string) {
    $db_driver = \Drupal::database()->driver();

    // Default encasing for PostgreSQL (and others ?) database driver.
    $encasing='"';
    // MySQL / MariaDB specific encasing.
    if($db_driver == 'mysql') {
      $encasing = '`';
    }

    $parts = explode(' = ', $string);
    foreach ($parts as &$part) {
      if (strpos($part, '.') !== FALSE) {
        $part = $encasing . str_replace('.', $this->subquery_namespace . $encasing . '.', $part);
      }
    }

    return implode(' = ', $parts);
  }

I posted a patch file below for those willing to try this.

I hope that a Drupal developer can do this in a better manner. The escaping of tables names should not be done by this plugin code.

landure’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

glynster’s picture

Removed

pooja saraah’s picture

Fixed failed commands on #55
Attached patch Against Drupal 9.5.x

pooja saraah’s picture

Status: Needs work » Needs review
nikhilraut’s picture

Assigned: Unassigned » nikhilraut
nikhilraut’s picture

Assigned: nikhilraut » Unassigned
Status: Needs review » Needs work
FileSize
2.49 MB
1.01 MB

After applying the patch 58 on drupal 9.5 same error is showing

nikhilraut’s picture

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

neclimdul’s picture

reroll of 50 after #3268818: Fix class comment doc blocks in non tests 'Drupal.Commenting.DocComment.ShortSingleLine' which included a different modification to a CS doc fix also included in this issue. Just accepted the upstream change. Merge diff is just the top line of the GroupwiseMax.php class doc.

Still needs tests though... I'm sure there's some pretty straightforward method of creating the view from the IS and running the preview to see the failure. Don't have time at the moment though. Would really appreciate someone starting that test.

glynster’s picture

@neclimdul updated patch is working well for us:

ENV info:
Drupal: 9.4.8
PHP: 8.1.12

neclimdul’s picture

FileSize
5.69 KB

blind 10.1 reroll. conflict was just a block in the GroupWiseMax and and it was breaking with the str_contains changes so the patch doesn't have an interdiff because the patch uses preg instead of the str_contains logic.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Carlos Romero made their first commit to this issue’s fork.