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

Comments

tedbow created an issue. See original summary.

tedbow’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new3.04 KB

Patch attached

tedbow’s picture

Issue summary: View changes
tedbow’s picture

For background I found this bug when a Scheduled Updates update runner plugin that will be add to the Workbench Moderation module.

I need to find all node revisions(or other entities) that reference Schedule Updates(non-revisionable entity) where the "update timestamp" is less that now. This works if am not looking for revisions and also works with patch in #2 applied.

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.

tedbow’s picture

Assigned: tedbow » Unassigned

Un-assigning myself. Still think this is an issue and would be great to get a review.

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.

dunebl’s picture

patch #2 doesn't apply properly on 8.5

patching file core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
Hunk #1 succeeded at 66 (offset -1 lines).
Hunk #2 FAILED at 88.
Hunk #3 FAILED at 157.
Hunk #4 succeeded at 271 with fuzz 2 (offset 65 lines).

Here is how I got this error:

[file_field_paths module is enable]
1- edit user's image field property: admin/config/people/accounts/fields/user.user.user_picture
2-in the "File (Field) Path settings" check "Retroactive update"
3-save the form
4-I got the following error:

Drupal\Core\Entity\Query\QueryException: '' not found in Drupal\Core\Entity\Query\Sql\Tables->ensureEntityTable() (line 316 of core/lib/Drupal/Core/Entity/Query/Sql/Tables.php). 
Drupal\Core\Entity\Query\Sql\Tables->addField('', 'INNER', NULL) (Line: 44)
Drupal\Core\Entity\Query\Sql\Condition->compile(Object) (Line: 155)
Drupal\Core\Entity\Query\Sql\Query->compile() (Line: 74)
Drupal\Core\Entity\Query\Sql\Query->execute() (Line: 263)
filefield_paths_batch_update(Object) (Line: 238)
filefield_paths_form_submit(Array, Object)
call_user_func_array('filefield_paths_form_submit', Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('field_config_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('field_config_edit_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 153)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
amateescu’s picture

Status: Needs review » Needs work

@tedbow, I'm not sure the variable name should be changed from all_revisions to use_revisioin, it seems to me that the only real change needed is the last hunk from the patch :)

Also, as noted in #10, this patch needs a reroll and some test coverage.

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.

wim leers’s picture

Issue tags: +Entity query
esod’s picture

StatusFileSize
new2.62 KB

Rerolled the patch for Drupal 8.6.x. Unfortunately, it doesn't solve our problem.

We have a problem similar to this with one of our custom modules. The module added a boolean field to the node_field_data table. During cron the value of the field is checked and act on (or not) accordingly. In hindsight it was not a good idea to use node_field_data, but since the custom module does things on every node on the site, it seemed like a good idea at the time. Before Drupal 8.5.x and content moderation, it worked fine.

I'll probably fix the problem by creating a custom table for the boolean field and the node_id. The custom field is not revisionable, which is the cause of my error.

My error message:

ResponseText: The website encountered an unexpected error. Please try again later.Drupal\Core\Entity\Query\QueryException: 'CUSTOM_FIELD_NAME' not found in Drupal\Core\Entity\Query\Sql\Tables->ensureEntityTable() (line 350 of core/lib/Drupal/Core/Entity/Query/Sql/Tables.php). Drupal\Core\Entity\Query\Sql\Tables->addField('CUSTOM_FIELD_NAME', 'LEFT', NULL) 
hchonov’s picture

Issue tags: +Needs tests

@esod, could you please provide more details? How exactly do you add the field? Is it translatable? Is it revisionable? Does it have cardinality? I think that your problem deserves a separate issue.

Also it would be nice if you could address @amateescu's remarks from #11:

@tedbow, I'm not sure the variable name should be changed from all_revisions to use_revisioin, it seems to me that the only real change needed is the last hunk from the patch :)

hchonov’s picture

Version: 8.5.x-dev » 8.7.x-dev
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.26 KB

As I am currently working on issues in this area I've decided to write a test for the issue here, but unfortunately my test isn't failing. So did the issue go away in the meanwhile? Do we still want to commit this test ensuring this won't break again?

Status: Needs review » Needs work

The last submitted patch, 16: 2649268-16-test-only.patch, failed testing. View results

esod’s picture

@hchonov, Sure. We added the field with this code in the module file.

/**
 * Implements hook_entity_base_field_info().
 *
 * Adds a boolean field to the node_field_data table.
 */
function MY_MODULE_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'node') {
    $fields['my_field'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('My Field'))
      ->setDescription(t('A boolean indicating when suggested terms are in the node.'))
      ->setDefaultValue(FALSE)
      ->setCardinality(1)
      ->setCustomStorage(TRUE);
    return $fields;
  }
  return NULL;
}

As you can see, the cardinality is set to 1. The field is a boolean whose only content can be NULL, 0, or 1 so we didn't think about translations, nor do I think we need to. The fact that this field is not revisionable is why my custom module is now broken. :-\

esod’s picture

StatusFileSize
new962 bytes

Per #11 here's a version of the reroll without changing $all_revisions to $use_revisions. Although since we're talking about using revisions, isn't it a good idea to keep the variable name change? Fwiw.

tedbow’s picture

Status: Needs work » Needs review

Setting to "Needs Review" to test #19

hchonov’s picture

@tedbow, could you please confirm if the bug you've reported is still present? I think that the test from #16 is testing exactly this, but it isn't failing.

The last submitted patch, 16: 2649268-16-test-only.patch, failed testing. View results

tedbow’s picture

I don't think the bug still exists.
I didn't ride a test at the time but I did make a test module: https://github.com/tedbow/d8_core_test

It doesn't through the error any more.

I wonder if this issue fixed it: #2700261: allRevisions() entity queries ignore non-revisionable fields

hchonov’s picture

StatusFileSize
new3.26 KB

@esod, do you mind opening a new issue for your problem?

@tedbow, do you think that the attached test reflects the initial problem described here? If so, do you think it is useful to commit it or simply close the issue? I personally think the more tests we have in that the direction the better.

P.S. I think that the issue you've referenced - #2700261: allRevisions() entity queries ignore non-revisionable fields - has solved the problem indeed.

tedbow’s picture

@hchonov I do think it would be good to have a test for this. Because it wasn't actually fixed on purpose so it could easily break again if something else changes and we don't have a test.

I haven't had time to look at the test in detail but in the comments of the test I don't think it says it explicitly testing this from my original summary:

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

I think the we should explicitly state so the test doesn't get change in the future or if users because revisionable the test won't cover it anymore.

so actually is there test entity type that is explicitly not revision-able that we would join with?

hchonov’s picture

I think the we should explicitly state so the test doesn't get change in the future or if users because revisionable the test won't cover it anymore.

I can add this to the comments.

so actually is there test entity type that is explicitly not revision-able that we would join with?

This is already happening in the test, as it is adding a condition on the name of a referenced user entity and the user entity type isn't revisionable. Beside it there is the entity_test entity type, which isn't revisionable as well.

esod’s picture

@hchonov, would you please explain how entity_test is not revisionable? Does your patch make that clearer? Does your patch make it clearer that the user entity entity type isn't revisionable? I'm curious to know.

Btw, I fixed my module. I changed ->setCustomStorage(TRUE) to ->setCustomStorage(FALSE) since I'm not using custom storage. I also added ->setRevisionable(FALSE) to be sure. I'd like my field to be revisionable, but it's not necessary for our current needs.

I didn't mean for my problem to be an issue for the community. I was just looking for answers and came across this issue. Thanks.

dawehner’s picture

StatusFileSize
new2.97 KB
new2.28 KB

Trying out this patch on my jsonapi usage didn't fully fixed the issue. It still tries to join to the wrong table.
Here is a patch which helped with that.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dunebl’s picture

Issue tags: +Needs reroll

Needs reroll for 8.7-dev

patching file core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
Hunk #1 succeeded at 190 (offset 16 lines).
Hunk #2 FAILED at 278.
Hunk #3 FAILED at 359.
ajits’s picture

Status: Needs review » Needs work

Updating to correct status based on #30.

rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new2.99 KB
ajits’s picture

Issue tags: -Needs reroll

@rpayanm - Thank you for the reroll. It is advisable to remove the "Needs reroll" tag when the reroll is done.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

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.

jnicola’s picture

Whoops, wrong issue! Sorry!

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.

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.

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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.