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.
Comment | File | Size | Author |
---|---|---|---|
my_field.zip | 1.02 KB | drikc |
Comments
Comment #1
drikc CreditAttribution: drikc commentedI'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):
Or perhaps a less clean work around is to remove the field table by hand with db_drop_table():
Comment #2
yched CreditAttribution: yched commentedduplicate of #943772: field_delete_field() and others fail for inactive fields, which is currently, er, a little bit stalled :-/