Integrity constraint violation, duplicate entry when creating book pages

Created on 20 December 2024, 4 days ago

Problem/Motivation

When creating a book page on a content type that is configured with an entity redirect, an WSOD occurs with the following error:

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '13' for key 'PRIMARY': INSERT INTO "book" ("nid", "bid", "pid", "weight", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "depth") 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, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13); Array ( [:db_insert_placeholder_0] => 13 [:db_insert_placeholder_1] => 13 [:db_insert_placeholder_2] => 0 [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => 13 [:db_insert_placeholder_5] => 0 [:db_insert_placeholder_6] => 0 [:db_insert_placeholder_7] => 0 [:db_insert_placeholder_8] => 0 [:db_insert_placeholder_9] => 0 [:db_insert_placeholder_10] => 0 [:db_insert_placeholder_11] => 0 [:db_insert_placeholder_12] => 0 [:db_insert_placeholder_13] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Steps to reproduce

Enable Entity Redirect for a content type that is also allowed to be a book page. For us, the redirect takes us to the Layout Builder Page. We are also not using the content type of "Book Page" that ships with the book module, but instead our own content type configured under /admin/structure/book/settings

Proposed resolution

In the entity_redirect.module file, in the function entity_redirect_submit() there is a line that calls $entity->save();. I believe this is causing the book entity to be saved twice with the same ID as the book module already calls that method ahead of the entity redirect module. My proposed resolution is to:

  1. Test if the book module is installed
  2. Test if the entity being saved is a book page
  3. If so, do not call this $entity->save() method here

I have tested this and it appears as though the book and node save fine and the redirect occurs as it should. I have forthcoming MR that will address this.

Remaining tasks

Create the MR with the change.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Active

Version

2.3

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States codechefmarc

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

Comments & Activities

Production build 0.71.5 2024