Problem/Motivation

I want to export/import a default node and/or taxonomy term containing a user reference field, which will contain the default value of admin (1). The import fails if the user already exists.

> In StatementWrapperIterator.php line 111:
>                                                                                                          
>   SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin-en' for key 'user__name'

Steps to reproduce

  1. Add a user reference field to a node type
  2. Create a node that references the admin user from the user reference field
  3. Export the node from one environment
  4. Import the node in another
  5. Experience the error

Proposed resolution

Allow content that references a user to be imported without trying to create users that already exist (maybe reference users by entity id)

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

MegaKeegMan created an issue. See original summary.

vensires’s picture

Issue tags: +GreeceSpringSprint2024
kyriazo’s picture

Assigned: Unassigned » kyriazo
kyriazo’s picture

Status: Active » Postponed (maintainer needs more info)

Tested on Drupal 10.2 with php 8.2 and works correctly. Can you give more feedback in order to reproduce the issue? Can you reproduce it on a clean environment?

megakeegman’s picture

Yes. The issue is that the uuid of the admin user is different between instances, so it tries to create a new user with the same name. So I guess I could just update the uuid to reflect the admin on the separate instance. Just a minor inconvenience.

megakeegman’s picture

I am trying to use this to generate default content for a distribution though. I could write a script that gets the uuid of the admin, updates the uuid in each default content zip, and then imports. But I guess the alternative is whether the import needs to respect the uuid of the referenced user, or if it could just infer that information based on the user with the given username. Any different ideas or suggestions?

megakeegman’s picture

Maybe a big ask but a token would be cool

nginex’s picture

Assigned: kyriazo » nginex
Status: Postponed (maintainer needs more info) » Needs work

I think this should be improved in doImport() method, at least we can have an extra validation by loading users by email and username if entity by uuid was not found. I will provide a patch, once it's available, there will be nothing to do on your side, I'm also planning to provide a new release soon, so the patch will be included to the new release immediately

nginex’s picture

Version: 2.0.x-dev » 1.4.x-dev
uri_frazier’s picture

I'm running into the same type of error, but in involves a complex node bundle type with a lot of fields.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'e72b42aa-af28-4aeb-80c3-4cb1e4e20ed8' for key 'paragraph_field__uuid__value': INSERT INTO "paragraphs_item" ("revision_id", "type", "uuid", "langcode") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => accordion [:db_insert_placeholder_2] => e72b42aa-af28-4aeb-80c3-4cb1e4e20ed8 [:db_insert_placeholder_3] => en ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of /mnt/alidata/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

#0 /mnt/alidata/web/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()
#1 /mnt/alidata/web/modules/contrib/single_content_sync/src/ContentImporter.php(247): Drupal\Core\Entity\EntityBase->save()
#2 /mnt/alidata/web/modules/contrib/single_content_sync/src/ContentImporter.php(202): Drupal\single_content_sync\ContentImporter->createOrUpdate()
#3 /mnt/alidata/web/modules/contrib/single_content_sync/src/Plugin/SingleContentSyncFieldProcessor/EntityReference.php(170): Drupal\single_content_sync\ContentImporter->doImport()

dlevchik made their first commit to this issue’s fork.

dlevchik’s picture

Status: Needs work » Needs review
Issue tags: -GreeceSpringSprint2024

Added check, if it is a user import, try to find it by properties, not only by uuid

But I think now, should it be only for a user? Right after loadEntityByUuid failed, we can do something like dispatch FindExistingEntity event, with content and entity type/storage, and modules can subscribe to it to fix those "Duplicate entry" messages. This should not be limited to the user, as user is only one of examples

nginex’s picture

Issue tags: +LutskGCW25
borg_git’s picture

StatusFileSize
new101.45 KB

Checked on the original code.

I created several taxonomy terms with references to users
exported, deleted terms and imported in various ways (via file and drush).
I was unable to reproduce any errors.

Drupal core version 10.2.6
Single Content Sync 1.4.8

nginex’s picture

Assigned: nginex » Unassigned
Status: Needs review » Needs work

For user entity I would definitely try to find a user by username if no match by uuid.

The issue is that user entity should have a unique username and if user A was create on site A and the same user was manually created on site B, they do have different uuid but the same username

du_paludo’s picture

StatusFileSize
new1.18 KB

I'm attaching an updated patch for the branch 1.4.x.

nginex’s picture

Status: Needs work » Needs review

  • nginex committed 32c5c48f on 1.4.x
    Issue #3431987: Try to find user by properties if it is not found by...
nginex’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.