It appear that when creating a field of a type defined by a custom field schema this field cannot be successfully deleted. That is, trying to create a field after deleting it output the following error message:
DatabaseSchemaObjectExistsException: Table <em class="placeholder">field_data_field_my_field</em> already exists. in DatabaseSchema->createTable() (line 630 of C:\Users\admin\www\drupal7\includes\database\schema.inc).

I've done a small module example (attached to this issue) that trigger this error. It use:
- hook_install() where field_create_field($field_name) is called.
- hook_field_schema($field) where the custom field schema is defined.
- hook_uninstall() where field_delete_field($field_name) is called.

So installing the module first time will work fine, but trying to reinstall will trigger the error.

I've notice that when using field_create_field($field) with the parameter 'type' with an existent schema (note defined in the custom module hook_field_schema($field)) this problem doesn't appear.

CommentFileSizeAuthor
my_field.zip1.02 KBdrikc
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drikc’s picture

Status: Closed (duplicate) » Active

I've found out that before the module hook_uninstall() is executed, the relative row field 'active' of the table 'field_config' is set to the value 0.

The consequence is that when calling the function field_info_field($field_name) in hook_uninstall() which indirectly call field_read_fields($field_name) the custom field isn't returned because field_read_fields($field_name) ignore fields that are set to 'inactive' (rows with 'active' field set to 0 in table 'field_config').

That's why field_delete_field($field_name) called in hook_uninstall() miss to delete the relative custom field and then the exception 'DatabaseSchemaObjectExistsException: Table field_data_field_my_field already exists' get fired at re-install.

A work around is to set back the 'active' field of table 'field_config' to 1 just before using field_delete_field($field_name):

/**
 * Implements hook_install().
 */
function my_field_uninstall() {
  $field_name = 'field_my_custom_field';
  db_update("field_config")
    ->fields(array(
      'active' => 1,
    ))
    ->condition(db_and()
      ->condition('field_name', $field_name, '=')
      ->condition('deleted', 0, '=')
    )
    ->execute();
  field_delete_field($field_name);
}

Or perhaps a less clean work around is to remove the field table by hand with db_drop_table():

/**
 * Implements hook_install().
 */
function my_field_uninstall() {
  $field_name = 'field_my_custom_field';
  field_delete_field($field_name);
  db_drop_table("field_data_$field_name");
  db_drop_table("field_revision_$field_name");
}
yched’s picture

Status: Active » Closed (duplicate)

duplicate of #943772: field_delete_field() and others fail for inactive fields, which is currently, er, a little bit stalled :-/

Status: Active » Closed (duplicate)