EntityStorageException "Duplicate entry" is thrown when creating a comment in hook_entity_type_insert

Created on 29 October 2015, over 9 years ago
Updated 19 May 2025, about 13 hours ago

Problem/Motivation

Create a comment entity for entity_test in a hook_entity_test_insert and you get Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry.
The problem is that the comment module implements the hook_entity_insert and creates for each created entity that supports comments an entry in the comment statistics table. But the hook_entity_type_insert runs before the hook_entity_insert and in Comment::postSave the entry in the comment statistics table will be created. After that the comment_entity_insert will try as well to create the entry which already exists.

Proposed resolution

Comment::postSave should first check if there is an entry in the comment statistics table, if not create an empty one and then update it.
comment_entity_insert should as well first check if there are no entries for the entity and only then create an empty entry.

Remaining tasks

none

User interface changes

none

API changes

none

Data model changes

none

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component

comment.module

Created by

πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

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.

  • πŸ‡ΊπŸ‡ΈUnited States ccjjmartin Austin, TX

    Still here in May of 2025, number 33 fixes the issue for me. Ran into this while doing a migration from 7 to 10.2.5 (the patch applies cleanly to this version even though it is 5+ years old). This bug actually prevented nodes from being created in the migration because of the error on the duplicate entry.

Production build 0.71.5 2024