Duplicate Profile Entry When Order Trying to Save Shipments

Created on 24 May 2024, about 1 month ago
Updated 14 June 2024, 14 days ago

Problem/Motivation

When i try to checkout, I am facing an issue in drupal 10.2.5 and commerce_shipping version 8.x-2.9

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '13109' for key 'PRIMARY': INSERT INTO "profile" ("profile_id", "revision_id", "type", "uuid", "status", "uid", "is_default", "data", "created", "changed") 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); Array ( [:db_insert_placeholder_0] => 13109 [:db_insert_placeholder_1] => 13109 [:db_insert_placeholder_2] => customer [:db_insert_placeholder_3] => cf849757-d6f0-467a-aa41-dfc4b4489ec7 [:db_insert_placeholder_4] => 1 [:db_insert_placeholder_5] => 135275 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => a:1:{s:23:"address_book_profile_id";s:5:"13109";} [:db_insert_placeholder_8] => 1710227063 [:db_insert_placeholder_9] => 1710227063 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Drupal\Core\Database\StatementWrapperIterator->execute() (Line: 44)
Drupal\mysql\Driver\Database\mysql\Insert->execute() (Line: 948)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems() (Line: 718)
Drupal\Core\Entity\ContentEntityStorageBase->doSave() (Line: 486)
Drupal\Core\Entity\EntityStorageBase->save() (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 319)
Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem->preSave() (Line: 263)
Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem->preSave() (Line: 233)
Drupal\Core\Field\FieldItemList->delegateMethod() (Line: 191)
Drupal\Core\Field\FieldItemList->preSave() (Line: 938)
Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod() (Line: 888)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook() (Line: 56)
Drupal\commerce\CommerceContentEntityStorage->invokeHook() (Line: 529)
Drupal\Core\Entity\EntityStorageBase->doPreSave() (Line: 753)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save() (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 55)
Drupal\commerce_shipping\LateOrderProcessor->process() (Line: 196)
Drupal\commerce_order\OrderRefresh->refresh() (Line: 123)
Drupal\commerce_order\OrderStorage->doOrderPreSave() (Line: 86)
Drupal\commerce_order\OrderStorage->invokeHook() (Line: 529)
Drupal\Core\Entity\EntityStorageBase->doPreSave() (Line: 753)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save() (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (Line: 169)
Drupal\commerce_order\OrderStorage->save() (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 358)
Drupal\commerce_shipping\Plugin\Commerce\CheckoutPane\ShippingInformation->buildPaneForm() (Line: 546)
Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowWithPanesBase->buildForm()
call_user_func_array() (Line: 536)
Drupal\Core\Form\FormBuilder->retrieveForm() (Line: 283)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 224)
Drupal\Core\Form\FormBuilder->getForm() (Line: 143)
Drupal\commerce_checkout\Controller\CheckoutController->formPage()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 48)
Drupal\redirect_after_login\RedirectMiddleware->handle() (Line: 54)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle() (Line: 58)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 43)
Drupal\webform_product\RedirectMiddleware->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 704)
Drupal\Core\DrupalKernel->handle() (Line: 19)

I found out its happening when order is trying to save shipments in file commerce_shipping/src/Plugin/Commerce/CheckoutPane/ShippingInformation.php --> buildPaneForm line number349

 // Update the shipments and save the order if no rate was explicitly

    // selected, that usually occurs when changing addresses, this will ensure

    // the default rate is selected/applied.

    if (!$this->hasRateSelected($pane_form, $form_state) && ($recalculate_shipping || $force_packing)) {

      array_map(function (ShipmentInterface $shipment) {

        if (!$shipment->isNew()) {

          $shipment->save();

        }

      }, $shipments);

      $this->order->set('shipments', $shipments);

      $this->order->save(); // its causing issues


🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇳🇵Nepal lalustine

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

Comments & Activities

  • Issue created by @lalustine
  • 🇮🇱Israel jsacksick

    I'd be a major issue if there was a way to constantly reproduce the issue. This isn't happening on a Vanilla install, otherwise tests would be failing.
    Do you have any custom code that could be causing this?

  • 🇳🇵Nepal lalustine

    I checked it but could not find any custom code that's causing an issue. It's just pointing me to commerce shipping code as you can see in above errors.

  • 🇳🇵Nepal lalustine

    I can see the similar issue raised by another person.
    https://www.drupal.org/project/commerce_shipping/issues/3309439 🐛 Race condition: new profile gets saved twice in ShippingInformation::validatePaneForm(), resulting in duplicate UUID exception Active

  • 🇮🇱Israel jsacksick

    You mentioned on the other duplicate issue you've created being logged in... I'm not sure that has any impact at all... The profile is owned by the shipment, it has a uid = 0... In other words, it is NOT attached to the currently logged in user, but to the shipment.
    Is this happening for all of your orders? Did you try checking out as another user? Or as anonymous?

  • 🇺🇦Ukraine tBKoT

    Hi @lalustine,
    Based on the error log you provided, a couple of the contrib modules are used. Unfortunately, we cannot reproduce this issue as we do not have the same site setup as yours, especially for the webform_product where actual "magic" for creating an order with a billing profile happens.

  • 🇳🇵Nepal lalustine

    Hi jsacksick, as i debugged I found that the shipment needs a shipment profile and when you are logged in already created shipment is passed. And I found another interesting thing that $shipping_profile->isNew() is giving true or 1 for the already existing shipping profile. Currently orders with shipment are only failing for me.
    With anonymous users, it's not breaking and everything is fine and new shipping profile is created.
    I don't think I have used any custom code which is affecting it. I can see whenever order is trying to save shipment with existing shipping profile, its failing in commerce shipping module. May be $shipping_profile->isNew() giving true or 1 for the already existing shipping profile can be the culprit which is telling to create profile for already existing profile. I experienced the same issue with other orders but I found the place to fix it by checking profile exit or not. But in this shipment scenario, I could find the place to add logic for that.

  • 🇳🇵Nepal lalustine

    Hi tBKoT, all i can see is commerce_checkout,commerce_order which is commerce core module and commerce_shipping only.

  • 🇮🇱Israel jsacksick

    Did you review the comment from @tBKoT? Probaly temporarily disabling the webform_product module to see if this is still happening?

  • 🇳🇵Nepal lalustine

    Hi jsacksick, its not the issue with webform_product module. I have reverted back drupal from 10.2.5 to 9.5 with all modules and database as well. The issue is not there and when I tested $shipping_profile->isNew(), its giving false for already existing shipping profile which was not the case in drupal 10.2.5.

    Either drupal 10.2.5 have issue or my database had an issue as $shipping_profile->isNew() was giving true for already existing shipping profile every time. I will try to upgrade drupal once again and see if the issue comes again.

  • 🇬🇧United Kingdom ChristianSanders

    I'm experiencing this issue also. Drupal 10.2.6, all commerce modules up to date.

    It's only happening with Anon users - no other contrib modules in the stacktrace.
    The only custom code is from our own Shipping pane, but all it does is build the form from the parent pane (the commerce_shipping pane) $pane_form = parent::buildPaneForm($pane_form, $form_state, $complete_form); and then does some changes with some of the labelling. But other than that, it's a stock setup.

  • I'm also seeing similar error but on a different route when I proceed to checkout. However a lot of custom code in play. Will update if I track anything down.

Production build 0.69.0 2024