Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#8 | 3276456-8.patch | 1.44 KB | seanB |
Issue fork drupal-3276456
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 #3
DieterHolvoet CreditAttribution: DieterHolvoet at Minsky 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 CreditAttribution: ricovandevin at Finlet for iO, Erasmus University Rotterdam commentedThanks 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.Comment #6
ricovandevin CreditAttribution: ricovandevin at Finlet for iO, Erasmus University Rotterdam 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.