diff --git a/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php b/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php index 03c32c9..0be84dd 100644 --- a/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php +++ b/core/lib/Drupal/Core/Entity/Schema/ContentEntitySchemaHandler.php @@ -220,22 +220,22 @@ protected function getFieldSchemaData($field_name, array $field_schema, array $c $data = array(); foreach ($field_schema[$schema_key] as $key => $columns) { - // Similar to the database column naming, we use the field name as the - // index or unique key name for single-column fields and a combination of - // field and index or unique key name for multi-column fields. - $real_name = count($field_schema['columns']) == 1 - ? 'field__' . $field_name - : 'field__' . $field_name . '__' . $key; - + // To avoid clashes with entity-level indexes or unique keys we use + // "{$entity_type_id}_field__" as a prefix instead of just + // "{$entity_type_id}__". We additionally namespace the specifier by the + // field name to avoid clashes when multiple fields of the same type are + // added to an entity type. + $entity_type_id = $this->entityType->id(); + $real_key = "{$entity_type_id}_field__{$field_name}__{$key}"; foreach ($columns as $column) { // Allow for indexes and unique keys to specified as an array of column // name and length. if (is_array($column)) { list($column_name, $length) = $column; - $data[$real_name][] = array($column_mapping[$column_name], $length); + $data[$real_key][] = array($column_mapping[$column_name], $length); } else { - $data[$real_name][] = $column_mapping[$column]; + $data[$real_key][] = $column_mapping[$column]; } } } @@ -260,15 +260,16 @@ protected function getFieldForeignKeys($field_name, array $field_schema, array $ $foreign_keys = array(); foreach ($field_schema['foreign keys'] as $specifier => $specification) { - // Similar to the database column naming, we use the field name as the - // foreign key name for single-column fields and a combination of field - // and foreign key name for multi-column fields. - $real_name = count($field_schema['columns']) == 1 - ? 'field__' . $field_name - : 'field__' . $field_name . '__' . $specifier; - $foreign_keys[$real_name]['table'] = $specification['table']; + // To avoid clashes with entity-level foreign keys we use + // "{$entity_type_id}_field__" as a prefix instead of just + // "{$entity_type_id}__". We additionally namespace the specifier by the + // field name to avoid clashes when multiple fields of the same type are + // added to an entity type. + $entity_type_id = $this->entityType->id(); + $real_specifier = "{$entity_type_id}_field__{$field_name}__{$specifier}"; + $foreign_keys[$real_specifier]['table'] = $specification['table']; foreach ($specification['columns'] as $column => $referenced) { - $foreign_keys[$real_name]['columns'][$column_mapping[$column]] = $referenced; + $foreign_keys[$real_specifier]['columns'][$column_mapping[$column]] = $referenced; } } diff --git a/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php b/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php index 855a0dc..235ecf8 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Schema/ContentEntitySchemaHandlerTest.php @@ -220,22 +220,24 @@ public function testGetSchemaLayoutBase($uuid_key) { ), 'primary key' => array('id'), 'indexes' => array( - 'field__owner' => array('owner'), - 'field__translator' => array(array('translator', 10)), - 'field__location__city' => array( + 'entity_test_field__owner__target_id' => array('owner'), + 'entity_test_field__translator__target_id' => array( + array('translator', 10), + ), + 'entity_test_field__location__country_state_city' => array( 'location__country', 'location__state', array('location__city', 10), ), ), 'foreign keys' => array( - 'field__editor' => array( + 'entity_test_field__editor__user_id' => array( 'table' => 'users', 'columns' => array( 'editor' => 'uid', ), ), - 'field__editor_revision__editor' => array( + 'entity_test_field__editor_revision__user_id' => array( 'table' => 'users', 'columns' => array( 'editor_revision__target_id' => 'uid', @@ -345,7 +347,7 @@ protected function setUpFieldDefinitions() { ) ), 'indexes' => array( - 'city' => array('country', 'state', array('city', 10)), + 'country_state_city' => array('country', 'state', array('city', 10)), ), )); // Add a field with a foreign key. @@ -356,7 +358,7 @@ protected function setUpFieldDefinitions() { ), ), 'foreign keys' => array( - 'editor' => array( + 'user_id' => array( 'table' => 'users', 'columns' => array( 'target_id' => 'uid', @@ -375,7 +377,7 @@ protected function setUpFieldDefinitions() { ), ), 'foreign keys' => array( - 'editor' => array( + 'user_id' => array( 'table' => 'users', 'columns' => array( 'target_id' => 'uid', diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php index c53dc32..8eb1ac0 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/DefaultTableMappingTest.php @@ -264,4 +264,4 @@ protected function setUpDefinition(array $column_names) { return $definition; } -} \ No newline at end of file +}