Problem/Motivation

The latest revision views filter produces invalid SQL queries in some cases.

Steps to reproduce

  1. Create a view targeting paragraphs
  2. Add a reverse entity relationship with a node paragraphs field
  3. Add a latest revision filter to that relationship

Example raw SQL query, produced from the view as described above:

SELECT
	paragraph__field_day.field_day_value AS paragraph__field_day_field_day_value,
	paragraphs_item_field_data.id AS id,
	paragraphs_item_field_data.langcode AS paragraphs_item_field_data_langcode,
	field_content_paragraphs_item_field_data.nid AS field_content_paragraphs_item_field_data_nid
FROM
	paragraphs_item_field_data paragraphs_item_field_data
	LEFT JOIN node__field_content node__field_content ON paragraphs_item_field_data.id = node__field_content.field_content_target_id
		AND node__field_content.deleted = '0'
	LEFT JOIN node_field_data field_content_paragraphs_item_field_data ON node__field_content.entity_id = field_content_paragraphs_item_field_data.nid
	# Below it goes wrong, since field_content_paragraphs_item_field_data is a table alias
	LEFT JOIN field_content_paragraphs_item_field_data field_content_paragraphs_item_field_data__field_content_paragraphs_item_field_data ON field_content_paragraphs_item_field_data.nid = field_content_paragraphs_item_field_data__field_content_paragraphs_item_field_data.nid
		AND field_content_paragraphs_item_field_data__field_content_paragraphs_item_field_data.vid > field_content_paragraphs_item_field_data.vid
	LEFT JOIN paragraph__field_day paragraph__field_day ON paragraphs_item_field_data.id = paragraph__field_day.entity_id
		AND(paragraph__field_day.deleted = '0'
			AND paragraph__field_day.langcode = paragraphs_item_field_data.langcode)
WHERE (paragraphs_item_field_data.parent_id = '1868')
AND((paragraphs_item_field_data.status = '1')
AND(paragraphs_item_field_data.type IN('festival_day'))
AND(field_content_paragraphs_item_field_data__field_content_paragraphs_item_field_data.nid IS NULL))
ORDER BY
	paragraph__field_day_field_day_value ASC

Proposed resolution

Use the table name instead of the alias when joining.

CommentFileSizeAuthor
#8 3276456-8.patch1.44 KBseanB

Issue fork drupal-3276456

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

DieterHolvoet created an issue. See original summary.

DieterHolvoet’s picture

Status: Active » Needs review

I opened a merge request with a fix that works in my case, not sure if it's good enough to be a general fix.

Lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Looks like a good direction, we will need some test coverage for this though.

ricovandevin’s picture

Thanks for the initial work on this. In our case $relationship->table gives us the base table of the view, which is not what we need. So I'm afraid that the MR is not yet good enough to serve as a generic fix for the issue.

ricovandevin’s picture

For us $relationship->definition['base'] seems to do the trick. Not sure (yet) whether that is a generic solution though.

Version: 10.0.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

seanB’s picture

FileSize
1.44 KB

Attached is an updated patch for Drupal 10.2.

phthlaap made their first commit to this issue’s fork.