Migration Update "Integrity constraint violation"

Created on 3 January 2025, 3 months ago

Problem/Motivation

When updating the migration it fails to find a previous RegistrationSettings entity to update failing with this:

[error]  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'node-120502-en' for key 'registration_settings_field_data.registration__host_entity_unique': INSERT INTO "registration_settings_field_data" ("settings_id", "langcode", "entity_type_id", "entity_id", "status", "capacity", "open", "close", "send_reminder", "reminder_date", "reminder_template__value", "reminder_template__format", "maximum_spaces", "multiple_registrations", "from_address", "confirmation", "confirmation_redirect", "default_langcode", "registration_waitlist_enable", "registration_waitlist_autofill", "registration_waitlist_autofill_state", "registration_waitlist_capacity", "registration_waitlist_message_enable", "registration_waitlist_message__value", "registration_waitlist_message__format") 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, :db_insert_placeholder_15, :db_insert_placeholder_16, :db_insert_placeholder_17, :db_insert_placeholder_18, :db_insert_placeholder_19, :db_insert_placeholder_20, :db_insert_placeholder_21, :db_insert_placeholder_22, :db_insert_placeholder_23, :db_insert_placeholder_24); Array
(
    [:db_insert_placeholder_0] => 1707
    [:db_insert_placeholder_1] => en
    [:db_insert_placeholder_2] => node
    [:db_insert_placeholder_3] => 120502
    [:db_insert_placeholder_4] => 1
    [:db_insert_placeholder_5] => 250
    [:db_insert_placeholder_6] =>
    [:db_insert_placeholder_7] =>
    [:db_insert_placeholder_8] => 1
    [:db_insert_placeholder_9] => 2025-01-22T21:59:40
    [:db_insert_placeholder_10] => ''
    [:db_insert_placeholder_11] => basic_html
    [:db_insert_placeholder_12] => 1
    [:db_insert_placeholder_13] => 0
    [:db_insert_placeholder_14] => ''
    [:db_insert_placeholder_15] => Registration has been saved.
    [:db_insert_placeholder_16] =>
    [:db_insert_placeholder_17] => 1
    [:db_insert_placeholder_18] => 0
    [:db_insert_placeholder_19] => 0
    [:db_insert_placeholder_20] =>
    [:db_insert_placeholder_21] => 0
    [:db_insert_placeholder_22] =>
    [:db_insert_placeholder_23] =>
    [:db_insert_placeholder_24] =>
)
 (/var/www/html/public/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:817)

Steps to reproduce

drush mim d7_registration_settings --update

Proposed resolution

Set this as a support request mostly if someone else runs into it...
I will comment with how we solved it.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

3.3

Component

Miscellaneous

Created by

πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

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

Comments & Activities

  • Issue created by @joelpittet
  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    We created a custom destination plugin to lookup the value in the getEntity() method, the only suspicious part is I hardcoded english because we aren't migrating the langcode:

    <?php
    
    declare(strict_types=1);
    
    namespace Drupal\custom_migration\Plugin\migrate\destination;
    
    use Drupal\migrate\Attribute\MigrateDestination;
    use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
    use Drupal\migrate\Row;
    
    /**
     * The 'registration_settings' destination plugin.
     */
    #[MigrateDestination(
      id: 'registration_settings'
    )]
    final class RegistrationSettings extends EntityContentBase {
    
      /**
       * The entity type plugin id for RegistrationSettings entity.
       */
      protected static function getEntityTypeId($plugin_id): string {
        return 'registration_settings';
      }
    
      /**
       * {@inheritdoc}
       */
      protected function getEntity(Row $row, array $old_destination_id_values) {
        // Extract the keys to determine if the entity already exists.
        $entity_type_id = $row->getSourceProperty('entity_type');
        $entity_id = $row->getSourceProperty('entity_id');
        // Hardcode en if not available.
        $langcode = $row->getSourceProperty('langcode') ?? 'en';
    
        // Load the existing entity if it exists.
        $existing_entities = $this->storage->loadByProperties([
          'entity_type_id' => $entity_type_id,
          'entity_id' => $entity_id,
          'langcode' => $langcode,
        ]);
    
        // Get the first match, if any.
        $entity = reset($existing_entities);
    
        if ($entity) {
          // Allow updateEntity() to change the entity.
          $entity = $this->updateEntity($entity, $row) ?: $entity;
        }
        else {
          // Attempt to ensure we always have a bundle.
          if ($bundle = $this->getBundle($row)) {
            $row->setDestinationProperty($this->getKey('bundle'), $bundle);
          }
    
          // Stubs might need some required fields filled in.
          if ($row->isStub()) {
            $this->processStubRow($row);
          }
          $entity = $this->storage->create($row->getDestination());
          $entity->enforceIsNew();
        }
    
        return $entity;
      }
    }
    
  • Status changed to Needs review 7 days ago
  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    @joelpittet thanks for posting this, your destination plugin looks good, glad you found a solution. However, I was not able to reproduce the error in my test environment - do you have multiple languages installed on your site by chance.

  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    Yes and no:), we have language enabled because search_api or the solr extension requires it but we are doing that one to one, und to en.

    Do you think that is the ticket?

  • πŸ‡ΊπŸ‡ΈUnited States john.oltman

    Could be - I'll reach out on Slack

Production build 0.71.5 2024