Cloning original node multiple times with clone user enabled causes an error

Created on 21 January 2020, over 4 years ago
Updated 10 May 2024, about 2 months ago

If you are cloning one node multiple times and you select to clone its author as well it causes integrity constraint violation.
Reproducing steps:

  1. Create an article (call it original)
  2. Click clone
  3. Check checkbox to clone referenced entity: Author
  4. Go to original article
  5. Click clone
  6. Check checkbox again to clone referenced entity: Author
🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇸🇮Slovenia kbrodej

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇭Switzerland megadesk3000

    I have the same issue still in the 2.x Version of entity_clone.
    The issue I have is with cloning a user more than once.

    Steps to reproduce:
    1. Create a user with name "user_original"
    2. Clone the user the first time. No errors occur. The user gets cloned correctly.
    3. Clone the "original" user once again. Now I get the "integrity constrained violation"

    Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user_original_cloned-de' for key 'user__name': INSERT INTO "users_field_data" ("uid", "langcode", "preferred_langcode", "preferred_admin_langcode", "name", "pass", "mail", "timezone", "status", "created", "changed", "access", "login", "init", "default_langcode") 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, :db_insert_placeholder_14); 
    

    I w ill try to provide a patch, that makes the cloned username unique by adding a suffix to the cloned username if needed.

  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇮🇳India Rajeshreeputra Pune

    Patch works and I don't see the error anymore.
    below code snippet from MR loops 'n' number of time for given username in db, instead have query to get latest(stored) user starts with $username.
    ex. lwt say db has 5 user with as below

    1. original
    2. original_cloned
    3. original_cloned_1
    4. original_cloned_2
    5. original_cloned_3
    6. original_cloned_4

    here latest entry is original_cloned_4 then explode with '_' and get last item + 1, which will gives '5'.

    @megadesk3000 what you say about this approach?

    +  protected function getUniqueUserName($username) {
    +    $storage = $this->entityTypeManager->getStorage('user');
    +    $original = $username;
    +
    +    $suffix = 0;
    +    while ($matches = $storage->loadByProperties(['name' => $username])) {
    +      $username = $original . '_' . $suffix++;
    +    }
    +
    +    return $username;
    +  }
    +
  • Status changed to RTBC about 2 months ago
  • 🇮🇳India Rajeshreeputra Pune

    Verified and works as expected.

  • Pipeline finished with Skipped
    about 2 months ago
    #169485
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.2.x + Environment: PHP 8.2 & MySQL 8
    last update about 2 months ago
    30 pass
  • 🇮🇳India Rajeshreeputra Pune

    Merged, release to follow shortly.

Production build 0.69.0 2024