How to fix migrate query() that's breaking my migration? So I have a Drupal 2 Drupal (d2d) migration configured as follows: 

class RanchoAboutNodeMigration extends RanchoNodeMigration {
  public function __construct(array $arguments) {
    parent::__construct($arguments);

    ...

    // This is an entity reference field
    $this-&gt;addFieldMapping('field_attached_images', 'iid')
         -&gt;separator(',')
         -&gt;sourceMigration('Image');
    ...
  }

  protected function query() {
    // Get the default query 
    $query = parent::query();
    $query-&gt;leftJoin('image_attach','ia','n.nid = ia.nid');
    $query-&gt;addExpression('GROUP_CONCAT(DISTINCT ia.iid)', 'iid');
    $query-&gt;addField('ia', 'iid', 'iid');
    $query-&gt;addField('ia', 'nid', 'inid');

    return $query;
  }      
}


The query causes two problems:


I can only import items one at a time through drush mi or the UI
Most important: I'll eventually run into a PDOexception for duplicate rows and then the error "Could not save to map table due to NULL value for key field nid" with migrate attempting to move totally empty rows.


Both items can be addressed by commenting out the line with $query-&gt;addExpression(), but then I miss some desired images. When the migration is actually working, everything migrates correctly, including the referenced image(s).

Any thoughts on what's causing this bizarre behavior when things go wrong and how to address it? 

Only thing I can think is to create a local query in prepareRow(), but I don't think I've seen anyone else do that before.
Adding a query to prepareRow() is the way to go:

  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }
    $query = Database::getConnection('default', 'd6source')
           -&gt;select('image_attach', 'ia')
           -&gt;fields('ia')
           -&gt;condition('nid',$row-&gt;nid);
    $query-&gt;addExpression('GROUP_CONCAT(DISTINCT ia.iid)', 'iid');
    $query-&gt;groupBy('ia.nid');
    $result = $query-&gt;execute();
    foreach ($result as $r) {
      $iid[] = $r-&gt;iid;
    }
    $row-&gt;iid = implode (",", $iid); 
  }


This has the huge added benefit of not messing up the source row count.