Entity Field Query throws QueryException when querying all revisions and setting condition on entity reference field where target doesn't support revisions

Created on 13 January 2016, about 9 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

When using Entity Field Query to query a revisionable entity and calling:
$query->allRevisions()
If you set a condition on an entity reference where the target entity type DOES NOT support revisions then the you get a fatal error form \Drupal\Core\Entity\Query\Sql\Tables::ensureEntityTable because \Drupal\Core\Entity\Query\Sql\Tables::addField will attempt to use the revision tables of the target entity even though they do not exist.

The simplest way to demonstrate this is to write a Entity Query to find all the node revisions that have a term in field_tags where the name equals a value.

$query = $this->entityTypeManager()->getStorage('node')->getQuery('AND');
    $query->condition('field_tags.entity.name', $search_str);
    $query->allRevisions();
    $ids = $query->execute();

This code throws the error.

Here is github demo repo that provides a module that demonstrates this problem.

Proposed resolution

Instead of directly using the "allRevision" metadata from the query use an $use_revisions variable which will be update if \Drupal\Core\Entity\Query\Sql\Tables::addField
determines the field is using a "valid relationship" through an entity reference field.

Remaining tasks

Complete patch
Write Tests

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated about 18 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024