Problem/Motivation

If we clone an entity that has the URI field and attempt to save, we get a fatal error.
ContentEntityBase::createDuplicate() copies all field values including the RDF URI field item list. When the duplicate is saved, assignUri() sees a non-empty $this->list and skips generation, then tries to INSERT the original URI for the new entity ID, causing a mysql integrity constraint violation.

Steps to reproduce

On an entity type that has rdf_sync uri_plugin active, try to duplicate it and save.

$term = Term::create(['vid' => 'category', 'name' => $this->randomString()]);
$term->save();
$duplicate = $term->createDuplicate();
$duplicate->save(); // throws integrity constraint violation
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'http://localhost/taxonomy-term/category/1e82d2b0-e18e-4817-a3...' for key 'uri': INSERT INTO "test73327032rdf_sync_uri" ("entity_type", "entity_id", "uri", "bundle") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array
(
[:db_insert_placeholder_0] => taxonomy_term
[:db_insert_placeholder_1] => 2
[:db_insert_placeholder_2] => http://localhost/taxonomy-term/category/1e82d2b0-e18e-4817-a3c0-bf0070d5...
[:db_insert_placeholder_3] => category
)

Proposed resolution

- Implement __clone() on RdfSyncUriFieldItemList to flush $this->list and reset $this->valueComputed when the field item list is cloned.
Tried it, doesn't work (wrong layer, fields is not reset on duplicated entity).

- Implement hook_entity_duplicate in rdf_sync.module to clear the URI field on the duplicate entity before it is saved.
This hook was only added since core 11.2.0 https://www.drupal.org/node/3268812
On earlier core versions, update RdfSyncUriFieldItemList::assignUri to clear the value if the URI already exists in DB.

Remaining tasks

?

User interface changes

None

API changes

None

Data model changes

None

Issue fork rdf_sync-3582431

Command icon 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

herved created an issue. See original summary.

herved’s picture

Title: Allow flushing the current URI when the entity property used to derive it changes » Integrity constraint violation when duplicating an entity with an RDF URI
Issue summary: View changes
herved’s picture

Status: Active » Needs review
herved’s picture

Issue summary: View changes
herved’s picture

Issue summary: View changes
herved’s picture

Issue summary: View changes
herved’s picture

Issue summary: View changes
herved’s picture

Issue summary: View changes
alorenc’s picture

Assigned: Unassigned » alorenc
alorenc’s picture

Assigned: alorenc » Unassigned
Status: Needs review » Needs work
alorenc’s picture

Status: Needs work » Reviewed & tested by the community

claudiu.cristea’s picture

Status: Reviewed & tested by the community » Fixed

Merged thank you

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.