Migration tries to create existing entity I have a D8 migration where I pull remote resources and for each one I create new entity object, apply the resource onto the entity via custom methods on that entity and return the data for this resource as $entity->toArray(). This is how my iterator is getting build. So basically it is an array of entity definitions.

On the target entity I have a field called 'name' which is a unique fields so no two entities with the same 'name' can exist. In the migration I have this field as id key. But for some reason when I run the migration it tries to create a new entity and so the UniqueField constraint will prevent the entity to be saved(I am doing validation in entity's preSave() method) and my migration will fail.

And I cannot figure out why it won't match the existing entity if the 'name' field has a value(I have checked this on multiple places in the code).

The only thing I can come up with it so try to load an existing entity in the prepareRow() myself but that seems as very strange approach and I could basically just completely scratch the migration and write a simple batch.



This is my iterator:

  /**
   * {@inheritdoc}
   */
  protected function initializeIterator() {
    $items = $this-&gt;getResourceEntity()-&gt;getResourcePlugin()-&gt;loadAll();
    $storage = $this-&gt;entityManager-&gt;getStorage($this-&gt;getDerivativeId());

    $items = array_map(function($resource) use ($storage) {
      $entity = $storage-&gt;create();
      $entity-&gt;resourceMap($resource);
      return $entity-&gt;toArray();
    }, $items);

    return new \ArrayIterator($items);
  }




After debugging \Drupal\migrate\Plugin\migrate\id_map\Sql::sourceIdFields and \Drupal\migrate\Plugin\migrate\id_map\Sql::destinationIdFields it looks like it tries to map 'name' =&gt; 'id' fields for some reason instead of 'name' =&gt; 'name'.

I guess that is because the destination plugin(content entity) returns the entity id field which in this case is id in \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::getIds.

I guess I am missing some crucial information here on how the migrations work.

So yeah, I had to use 'id' in getIds(), and in the iterator I had to try to figure out entity id of an existing entity.
It's rather premature to create an entity at the source plugin, when the destination plugin will create the entity for you... It's much simpler for the iterator to simply produce an array of fields representing the raw data, have the process pipeline map those source fields to the Drupal destination entity's field names, and have the destination plugin 'entity:my_entity' create and save the entity to Drupal.