9

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->map = new MigrateSQLMap($this->machineName,

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

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

$this->source = new MigrateSourceSQL($query);

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

$this->addFieldMapping('name', 'label');
$this->addFieldMapping('description')
    ->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.

2 Answers 2

30

Figured it out!

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

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

The 'alias' => 'l' makes the labelId become l.labelId in the queries.

3
  • Thanks. Had the same problem. You should mark your answer as correct.
    – Perisdr
    Dec 5, 2012 at 15:23
  • Same problem... and solution works perfectly. I've spent way too many hours on this one! Thank you for posting the fix.
    – sea26.2
    Jan 12, 2013 at 21:21
  • You deserve a medal for that tid bit.
    – doublejosh
    Oct 1, 2014 at 20:01
1

I thought that he was setting alias to 1 (ie, boolean true), but there's a reason it's a string - that is the actual alias, so if you're alias is n, 'alias' => 'n'. That actually cost me about 1/2 hr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.