Migrate failure: on clause is ambiguous I'm working with the migrate module for the first time and I've worked with the db_select api a little bit, but I'm running into an error.

I'm trying to convert some old labels into a drupal taxonomy. When I browse to the /admin/content/migrate page, it does my query correctly and shows the right number of rows that should be migrated. However, when I actually try to run the import, I get this error:


  Migration failed with source plugin exception: SQLSTATE[23000]:
  Integrity constraint violation: 1052 Column 'labelId' in on clause is
  ambiguous


Here is the code I have in my LabelMigration class:

$this-&gt;map = new MigrateSQLMap($this-&gt;machineName,

    array(
        'labelId' =&gt; array('type' =&gt; 'int', 'not null' =&gt; TRUE)
    ),
    MigrateDestinationTerm::getKeySchema()
);
$query = db_select('migrate_account_label', 'l')
         -&gt;fields('l', array('labelId', 'label'))
;

$query-&gt;leftJoin('migrate_asset_labels_membership', 'lm', 'l.labelId = lm.labelId');
$query-&gt;leftJoin('migrate_asset', 'a', 'lm.assetId = a.assetId');
$query-&gt;addExpression('count(*)', 'num');
$query-&gt;condition('a.type', 'B');
$query-&gt;groupBy('l.labelId');
$query-&gt;groupBy('l.label');

$this-&gt;source = new MigrateSourceSQL($query);

// Set up our destination - terms in the vocabulary
$this-&gt;destination = new MigrateDestinationTerm('article_labels');

$this-&gt;addFieldMapping('name', 'label');
$this-&gt;addFieldMapping('description')
    -&gt;defaultValue('');


If I remove the leftJoin() calls then obviously the error about the on statement goes away, but I don't understand how labelId is ambiguous because I'm always referencing it with a table alias.

Any ideas where this error is coming from? Is it coming from the MigrateSQLMap at the top? If so, how can I reference that labelId with an alias to make it non-ambiguous? I tried making it l.labelId, but that didn't work.
Figured it out!

When defining the field in the MigrateSQLMap I can set a table alias for the field:

$this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
    array(
        'labelId' =&gt; array(
            'type' =&gt; 'int',
            'not null' =&gt; TRUE,
            'alias' =&gt; 'l', // it's the letter small case "L", not the digit 1
        )
    ),
    MigrateDestinationTerm::getKeySchema()
);


The 'alias' =&gt; 'l' makes the labelId become l.labelId in the queries.