Problem/Motivation
When extending FileEntityItem with additional DB query, the migration will fail with source plugin exception due to ambiguous ON clause. This is caused by missing table alias in the getIds() method.
eg.
Migration failed with source plugin exception: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'fid' in on clause is ambiguous: SELECT `fm`.*, `fm`.`type` AS `type`, `map`.`sourceid1` AS `migrate_map_sourceid1`, `map`.`source_row_status` AS `migrate_map_source_row_status`, SUBSTRING(fm.uri, 1, POSITION('://' IN fm.uri) - 1) AS `scheme`
FROM
`file_managed` `fm`
LEFT OUTER JOIN `file_usage` `fu` ON fu.fid = fm.fid
LEFT OUTER JOIN db.migrate_map_hs_migrations_media_image `map` ON fid = map.sourceid1
WHERE (`fm`.`status` = :db_condition_placeholder_0) AND (`fm`.`uri` NOT LIKE :db_condition_placeholder_1 ESCAPE '\\') AND (`fm`.`type` <> :db_condition_placeholder_2) AND (`fu`.`count` > :db_condition_placeholder_3) AND (`fm`.`fid` NOT IN (SELECT `fu`.`fid` AS `fid`
FROM
`users` `u`
LEFT OUTER JOIN `file_usage` `fu` ON fu.fid = u.picture
WHERE (u.picture > 0)
GROUP BY fu.fid
HAVING (GROUP_CONCAT(DISTINCT fu.type) = :allowed_value_user_only OR GROUP_CONCAT(DISTINCT fu.type) = :allowed_value_user_webform_only OR GROUP_CONCAT(DISTINCT fu.type) = :allowed_value_webform_user_only))) AND (`fm`.`fid` NOT IN (SELECT `fu`.`fid` AS `fid`
FROM
`file_usage` `fu`
GROUP BY fu.fid
HAVING (GROUP_CONCAT(DISTINCT fu.type) = :allowed_type_submission_only OR GROUP_CONCAT(DISTINCT fu.type) = :allowed_type_submission_user_only OR GROUP_CONCAT(DISTINCT fu.type) = :allowed_type_user_submission_only))) AND (`fm`.`type` = :db_condition_placeholder_4) AND ((`map`.`sourceid1` IS NULL) OR (`map`.`source_row_status` = :db_condition_placeholder_5))
ORDER BY `fm`.`timestamp` ASC; Array
(
[:db_condition_placeholder_0] => 1
[:db_condition_placeholder_1] => temporary://%
[:db_condition_placeholder_2] => undefined
[:db_condition_placeholder_3] => 0
[:allowed_value_user_only] => user
[:allowed_value_user_webform_only] => user,webform
[:allowed_value_webform_user_only] => webform,user
[:allowed_type_submission_only] => submission
[:allowed_type_submission_user_only] => submission,user
[:allowed_type_user_submission_only] => user,submission
[:db_condition_placeholder_4] => image
[:db_condition_placeholder_5] => 1
)
in /var/www/html/web/core/lib/Drupal/Core/Database/Driver/mysql/ExceptionHandler.php line 50
Steps to reproduce
/**
* Drupal 7 file_entity source from database.
*
* @MigrateSource(
* id = "file_media_entity",
* source_module = "file_entity"
* )
*/
class FileMediaEntity extends FileEntityItem {
/**
* {@inheritdoc}
*/
public function query() {
$query = parent::query();
$query->leftJoin('file_usage', 'fu', 'fu.fid = fm.fid');
$query->condition('fu.count', 0, '>');
return $query;
}
}
Proposed resolution
Add
$ids['fid']['alias'] = 'fm';
API changes
None.
Comments
Comment #2
ckngComment #4
ckngComment #7
huzooka@ckng, thank you and apologize for the slow action.