diff --git a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
index e268c544a7db0a3872c75af90c38fc133a2c1faf..7977a0f05b6fd08dfdd3f96edb35b96a35da40fb 100644
--- a/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
+++ b/core/modules/migrate/src/Plugin/migrate/id_map/Sql.php
@@ -219,7 +219,7 @@ public function getQualifiedMapTableName() {
    * @return \Drupal\Core\Database\Connection
    *   The database connection object.
    */
-  protected function getDatabase() {
+  public function getDatabase() {
     if (!isset($this->database)) {
       $this->database = \Drupal::database();
     }
diff --git a/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php
index 4e10d45c798ec3b6eb0a9319da1a739abcb1d2f2..7c5688d97e3869cd5268f51574797f74ffc9ab69 100644
--- a/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php
+++ b/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php
@@ -9,6 +9,7 @@
 
 use Drupal\Core\Database\Database;
 use Drupal\migrate\Entity\MigrationInterface;
+use Drupal\migrate\Plugin\migrate\id_map\Sql;
 use Drupal\migrate\Plugin\MigrateIdMapInterface;
 
 /**
@@ -116,7 +117,7 @@ protected function runQuery() {
       //      OR above highwater).
       $conditions = $this->query->orConditionGroup();
       $condition_added = FALSE;
-      if ($this->getIds() && ($this->migration->getIdMap() instanceof \Drupal\migrate\Plugin\migrate\id_map\Sql)) {
+      if ($this->mapJoinable()) {
         // Build the join to the map table. Because the source key could have
         // multiple fields, we need to build things up.
         $count = 1;
@@ -192,4 +193,21 @@ public function getIterator() {
     return $this->iterator;
   }
 
+  protected function mapJoinable() {
+    if (!$this->getIds()) {
+      return FALSE;
+    }
+    $id_map = $this->migration->getIdMap();
+    if (!$id_map instanceof Sql) {
+      return FALSE;
+    }
+    $id_map_database_options = $id_map->getDatabase()->getConnectionOptions();
+    $source_database_options = $this->getDatabase()->getConnectionOptions();
+    foreach (array('username', 'password', 'host', 'port', 'namespace', 'driver') as $key) {
+      if ($id_map_database_options[$key] != $source_database_options[$key]) {
+        return FALSE;
+      }
+    }
+    return TRUE;
+  }
 }