Problem/Motivation
The latest revision views filter produces invalid SQL queries in some cases.
Steps to reproduce
- Create a view targeting paragraphs
- Add a reverse entity relationship with a node paragraphs field
- 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.
Comments
Comment #3
dieterholvoet commentedI opened a merge request with a fix that works in my case, not sure if it's good enough to be a general fix.
Comment #4
lendudeLooks like a good direction, we will need some test coverage for this though.
Comment #5
ricovandevin commentedThanks for the initial work on this. In our case
$relationship->tablegives 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.Comment #6
ricovandevin commentedFor us
$relationship->definition['base']seems to do the trick. Not sure (yet) whether that is a generic solution though.Comment #8
seanbAttached is an updated patch for Drupal 10.2.