How to reference existing taxonomy terms in a migration I'm trying to migrate nodes with a term reference field.  The terms are not migrated and already exist in the destination site.  There is a method in the migration class which is confirmed to already select the correct term ID on the new site.

When the migration is run an error appears:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'migration_db.taxonomy_index' doesn't exist:    [error]
SELECT 1 AS expression
FROM
{taxonomy_index} taxonomy_index
WHERE ( (nid = :db_condition_placeholder_0) AND (tid = :db_condition_placeholder_1) AND (status =
:db_condition_placeholder_2) ); Array
(
    [:db_condition_placeholder_0] =&gt; 2290
    [:db_condition_placeholder_1] =&gt; 4
    [:db_condition_placeholder_2] =&gt; 1
)
 (/home/vagrant/docroot/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:770)


The migration appears to be trying to get the term data from the migration database instead of the destination (default) database.

migrate plus yml:

...
process:
  field_section: Section
...


migration class:

class MyNodeMigration extends SqlBase {
  public function query() {
    return $this-&gt;select('TableA', 'ta')
      -&gt;fields('ta', ['Id', 'Body', 'OriginalId']);
  }    
 ...
  public function prepareRow(Row $row) {
    $row-&gt;setSourceProperty('Section', $this-&gt;convertToTermId($row-&gt;getSourceProperty('OriginalId'));
  }

  public function convertToTermId($original_id) {
    return $this-&gt;getDatabase()-&gt;select('TableB', 'tb')
      -&gt;fields('tb', ['DrupalTermId'])
      -&gt;condition('OldId', $original_id)
      -&gt;execute()
      -&gt;fetchField();
  }
}


-------------UPDATE-------------

I've also tried using a custom plugin.  The same error occurs.
Plugin class:

/**
 *
 * @MigrateProcessPlugin(
 *   id = "existing_term"
 * )
 */
class MigrateProcessExistingTerm extends ProcessPluginBase {
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $term = Term::load($value);
    return [
      'target_id' =&gt; $term-&gt;id(),
    ];
  }

}


Updated yml:

...
process:
  field_section:
    plugin: existing_term
    source: Section
...
I was able to solve the issue by moving the queries for the term ID to the process plugin.

/**
 *
 * @MigrateProcessPlugin(
 *   id = "existing_term"
 * )
 */
class MigrateProcessExistingTerm extends ProcessPluginBase {
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

    db_set_active('my_migration');

    $sub_task_id = db_select('TableA', 'ta')
      -&gt;fields('ta', ['OriginalId'])
      -&gt;condition('Id', $value)
      -&gt;execute()
      -&gt;fetchField();

    $term_id = db_select('TableB', 'tb')
      -&gt;fields('tb', ['DrupalTermId'])
      -&gt;condition('OriginalId', $original_id)
      -&gt;execute()
      -&gt;fetchField();

      db_set_active('default');

    $term = Term::load($term_id);

    return [
      'target_id' =&gt; $term-&gt;id(),
    ];
  }

}


It seems like having the queries in the main class were confusing the migration into thinking the term data was in the migration source database.