diff --git a/entityreference.install b/entityreference.install
index 4e248ef..1fbd66a 100644
--- a/entityreference.install
+++ b/entityreference.install
@@ -161,4 +161,39 @@ function entityreference_update_7002() {
       'not null' => TRUE,
     ));
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Update foreign keys where necessary.
+ */
+function entityreference_update_7003() {
+  // Load the base table configuration from the cache.
+  $base_tables = variable_get('entityreference:base-tables', array());
+
+  foreach (field_info_fields() as $field_name => $field) {
+
+    if ($field['type'] != 'entityreference') {
+      // Not an entity reference field.
+      continue;
+    }
+
+    // Read in the field config
+    $sql_field = field_read_field($field['field_name']);
+
+    // Create a foreign key to the target entity type base type, if available.
+    $entity_type = $sql_field['settings']['target_type'];
+    if (isset($base_tables[$entity_type])) {
+      list($base_table, $id_column) = $base_tables[$entity_type];
+      $schema['foreign keys'][$base_table] = array(
+        'table' => $base_table,
+        'columns' => array('target_id' => $id_column),
+      );
+    }
+
+    // Update foreign keys to actually contain the proper entity foreign keys if they differ.
+    if ($sql_field['foreign keys'] != $schema['foreign keys']) {
+      $sql_field['foreign keys'] = $schema['foreign keys'];
+      field_update_field($sql_field);
+    }
+  }
+}
