Issue with multiple paragraphs referencing same media

Created on 17 January 2024, 11 months ago
Updated 23 February 2024, 10 months ago

Problem/Motivation

Unique violation exception when saving node with multiple paragraphs referencing the same media.

Steps to reproduce

Add multiple paragraphs referencing the same media entity.
Try to save the node.
Error:
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "media_usage__media_reference__key" DETAIL: Key (mid, entity_type, bundle_name, eid, langcode, submodule)=(473, node, page, 950, de, meu_paragraphs) already exists.: INSERT INTO "media_usage" ("mid", "entity_type", "bundle_name", "eid", "langcode", "submodule") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5); Array ( ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Proposed resolution

I see two possible approaches:
1. Add "usage count" column to database table. And increase or decrease it when creating/updating/deleting paragraph.
2. Check if there is an usage record before adding new one. But in this case we will have to re-validate if there is another reference to this entity when deleting paragraph. Possible performance impact.

🐛 Bug report
Status

Active

Version

9.1

Component

Code

Created by

🇧🇾Belarus Yury N

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

Comments & Activities

Production build 0.71.5 2024