diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
index c8bf59d..882694d 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/Tables.php
@@ -6,6 +6,7 @@
 use Drupal\Core\Entity\Query\QueryException;
 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
 use Drupal\Core\Entity\Sql\TableMappingInterface;
+use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
 
 /**
  * Adds tables and fields to the SQL entity query.
@@ -253,10 +254,20 @@ public function addField($field, $type, $langcode) {
           $relationship_specifier = $specifiers[$key + 1];
           $next_index_prefix = $relationship_specifier;
         }
+        $entity_type_id = NULL;
+        // Relationship specifier can also contain the entity type id i.e.
+        // entity:node, entity:user or entity:taxonomy.
+        if (strpos($relationship_specifier, ':') !== FALSE) {
+          list($relationship_specifier, $entity_type_id) = explode(':', $relationship_specifier, 2);
+        }
         // Check for a valid relationship.
-        if (isset($propertyDefinitions[$relationship_specifier]) && $field_storage->getPropertyDefinition('entity')->getDataType() == 'entity_reference' ) {
-          // If it is, use the entity type.
-          $entity_type_id = $propertyDefinitions[$relationship_specifier]->getTargetDefinition()->getEntityTypeId();
+        if (isset($propertyDefinitions[$relationship_specifier]) && $propertyDefinitions[$relationship_specifier] instanceof DataReferenceDefinitionInterface) {
+          // If it is, use the entity type if specified already, otherwise use
+          // the definition.
+          if (!$entity_type_id) {
+            $entity_type_id = $propertyDefinitions[$relationship_specifier]->getTargetDefinition()
+              ->getEntityTypeId();
+          }
           $entity_type = $this->entityManager->getDefinition($entity_type_id);
           $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
           // Add the new entity base table using the table and sql column.
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
index 8465438..2cc9bf2 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryRelationshipTest.php
@@ -154,6 +154,45 @@ public function testQuery() {
       ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
       ->execute();
     $this->assertResults(array(1, 2));
+    // This returns the 0th entity as that's only one pointing to the 0th
+    // account.
+    $this->queryResults = $this->factory->get('entity_test')
+      ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername())
+      ->execute();
+    $this->assertResults(array(0));
+    // This returns the 1st and 2nd entity as those point to the 1st account.
+    $this->queryResults = $this->factory->get('entity_test')
+      ->condition("user_id.entity:user.name", $this->accounts[0]->getUsername(), '<>')
+      ->execute();
+    $this->assertResults(array(1, 2));
+    // This returns all three entities because all of them point to an
+    // account.
+    $this->queryResults = $this->factory->get('entity_test')
+      ->exists("user_id.entity:user.name")
+      ->execute();
+    $this->assertResults(array(0, 1, 2));
+    // This returns no entities because all of them point to an account.
+    $this->queryResults = $this->factory->get('entity_test')
+      ->notExists("user_id.entity:user.name")
+      ->execute();
+    $this->assertEqual(count($this->queryResults), 0);
+    // This returns the 0th entity as that's only one pointing to the 0th
+    // term (test without specifying the field column).
+    $this->queryResults = $this->factory->get('entity_test')
+      ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value)
+      ->execute();
+    $this->assertResults(array(0));
+    // This returns the 0th entity as that's only one pointing to the 0th
+    // term (test with specifying the column name).
+    $this->queryResults = $this->factory->get('entity_test')
+      ->condition("$this->fieldName.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
+      ->execute();
+    $this->assertResults(array(0));
+    // This returns the 1st and 2nd entity as those point to the 1st term.
+    $this->queryResults = $this->factory->get('entity_test')
+      ->condition("$this->fieldName.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
+      ->execute();
+    $this->assertResults(array(1, 2));
   }
 
   /**
