Fatal SQL error on parent delete when storage entity is referenced

Created on 9 September 2022, over 2 years ago
Updated 5 February 2025, 2 months ago

Problem/Motivation

I have a node with composite reference to a storage entity (results). If I am logged as not-uid 1 and delete the node, the site shows

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node_revision_field_results.nid' in 'where clause': SELECT "base_table"."vid" AS "vid", "base_table"."nid" AS "nid" FROM "node_revision" "base_table" LEFT JOIN "node_revision_field_results" "node_revision_field_results" ON "node_revision_field_results"."revision_id" = "base_table"."vid" WHERE ("node_revision_field_results"."field_results_target_id" = :db_condition_placeholder_0) OR (EXISTS (SELECT "na"."nid" AS "nid" FROM "node_access" "na" WHERE ((("gid" IN (:db_condition_placeholder_1)) AND ("realm" = :db_condition_placeholder_2)) OR (("gid" IN (:db_condition_placeholder_3)) AND ("realm" = :db_condition_placeholder_4)) OR (("gid" IN (:db_condition_placeholder_5)) AND ("realm" = :db_condition_placeholder_6)) OR (("gid" IN (:db_condition_placeholder_7)) AND ("realm" = :db_condition_placeholder_8))) AND ("na"."grant_view" >= :db_condition_placeholder_9) AND (("node_revision_field_results"."nid" = "na"."nid")))); Array ( [:db_condition_placeholder_0] => 528 [:db_condition_placeholder_1] => 0 [:db_condition_placeholder_2] => all [:db_condition_placeholder_3] => 1479 [:db_condition_placeholder_4] => view_unpublished_author [:db_condition_placeholder_5] => 1 [:db_condition_placeholder_6] => view_unpublished_published_content [:db_condition_placeholder_7] => 1 [:db_condition_placeholder_8] => view_unpublished_content [:db_condition_placeholder_9] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 759 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Drupal\Core\Database\StatementWrapper->execute(Array, Array) (Line: 937)

Steps to reproduce

1. Log in as user that is not uid 1 (so access checks are not bypassed).
1. Create content type with composite reference to storage entity.
1. Create node instance and related storage entity instance.
1. Delete node.
1. Observer error. (Expectation is that node and related storage entity are both deleted).

Proposed resolution

AFAICT during node deletion, composite reference does a select query to scan what entities are being referenced by the node being deleted. This query has alterTags node_access, entity_query, entity_query_node. The query is rewritten to do a node access check in a way that assumes a nid is being referenced when it should be entity_id (see node_revision_field_results.nid in error above).

One solution is to disable the access check by adding $query->accessCheck(FALSE); to line 79 in getReferencingEntities() in CompositeReferenceFieldManager.php.
"Access checking must be explicitly specified on content entity queries" https://www.drupal.org/node/3201242 is relevant.

Remaining tasks

Consider whether access check should be true or false.

🐛 Bug report
Status

Closed: outdated

Version

2.1

Component

Code

Created by

🇳🇿New Zealand jonathan_hunt

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