GroupRelationship::postDelete can try and save a deleted entity

Created on 4 June 2023, about 2 years ago
Updated 21 June 2024, about 1 year ago

Problem/Motivation

When deleting a group relationship the postDelete method retrieves the entity referenced from the relationship entity https://git.drupalcode.org/project/group/-/blob/1d7b4ee7d69b3bfcfde8cc79...
However if prior to this call the reference has been used to get the entity for whatever reason (just one example see steps to reproduce) it will be on the relationship entity even if in the database the referenced entity has since been deleted.

Steps to reproduce

Code I was using that stumbled on this was finding the entities to delete from their references. So:

$relationship = GroupRelationship::load($id);
$entity = $relationship->getEntity();
$entity->delete();

Run with $id being a gnode will cause errors:

[error]  Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported. in Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (line 762 of /app/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php). 
 [error]  Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of /app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). 
 [error]  Drupal\Core\Entity\EntityStorageException: Update existing 'node' entity while changing the ID is not supported. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 761 of /app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). 

As it tries to resave the already deleted entity.

Proposed resolution

Reloading the entity to check that it still exists works.

Remaining tasks

Patch/MR to follow.

πŸ› Bug report
Status

Active

Version

3.1

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands ekes

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

Comments & Activities

  • Issue created by @ekes
  • πŸ‡³πŸ‡±Netherlands ekes

    Patch first because I want it to apply to 2.0.x ;)
    But believe this holds for everything from 2.0.x - 3.1.x The patch itself was made and tested against 3.0.x

  • πŸ‡ΊπŸ‡ΈUnited States maskedjellybean Portland, OR

    Wow thank you for this! In my case this error was being thrown during a Behat afterScenario while attempting to delete nodes. Here is a version of the patch for Group 1.0.x (tested against 1.6.0) for those of us still stuck on a super outdated version.

Production build 0.71.5 2024