Drupal with PostgresSQL cloning an entity created using ECK.

<?php
replicate_entity_by_id('daas_site', 1);
?>
WD daas_site: PDOException: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "eck_daas_site_pkey"                                                                                                                                                             [error]
DETAIL:  Key (id)=(1) already exists.: INSERT INTO eck_daas_site

Here is a small diff that fixes this:

diff --git a/sites/all/modules/contrib/replicate/replicate.module b/sites/all/modules/contrib/replicate/replicate.module
index 53c6c25..a4563df 100755
--- a/sites/all/modules/contrib/replicate/replicate.module
+++ b/sites/all/modules/contrib/replicate/replicate.module
@@ -102,6 +102,9 @@ function replicate_clone_entity($entity_type, $entity) {
     drupal_alter('replicate_entity', $clone, $entity_type, $entity);
   }
 
+  $entity_info = entity_get_info($entity_type);
+  unset($clone->{$entity_info['entity keys']['id']});
+
   return $clone;
 }

Also, in the mean time a work around (currently untested) would be to add the same unsetting of the clone's id in HOOK_replicate_entity_alter()

Comments

thursday_bw’s picture

StatusFileSize
new552 bytes

Adding the previously mentioned in line diff as a patch file.

jgalletta’s picture

StatusFileSize
new1.49 KB

I can't believe we don't use this code to clean entities, that's simpler than our current code...
I made a new patch using your code, I moved the 2 lines just before sending entities to other modules, as cleaning the entity id is something we want to do for all entities.

I also removed the implemented hooks that become useless with the new code.

jgalletta’s picture

Status: Active » Needs review
thursday_bw’s picture

jgalletta,

Awesome stuff, thank your for following up on this.

vbouchet’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new2.4 KB

This is great as custom entities which are properly defined and list their "entity keys" will be automatically prepared for replication.

I edited the hook_replicate_entity_ENTITY_TYPE() documentation as it is not supposed to be required to implement the hook to prepare custom entities anymore. (Feel free to use the previous patch if you think it still makes sense to describe it.

  • thursday_bw authored 0e2c551 on
    Issue #2536714 by thursday_bw, jgalletta, vbouchet: Clean entity id in a...
jgalletta’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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