How to change the machine name of a content field in Drupal 7

Once a new content field is created, there is no way to change its machine name from administration area. No worries though, this still could be done quite easily by only few updates directly to the database.

Once a new content field is created, there is no way to change its machine name from administration area. No worries though, this still could be done quite easily by only few updates directly to the database. Remember however to always dump a backup copy of your database just in case things go wrong, and be really careful when executing those changes!

Let's assume you want to change Drupal 7's default field field_tags and rename it to singular field_tag. What you need to do then is:

  • in table field_config - update old field name to a new one in field_name column:

    UPDATE field_config SET field_name = 'field_tag' WHERE field_name = 'field_tags';
    
  • do the same in table field_config_instance:

    UPDATE field_config_instance SET field_name = 'field_tag' WHERE field_name = 'field_tags';
    
  • rename table field_data_field_tags to field_data_field_tag:

    RENAME TABLE `field_data_field_tags` TO `field_data_field_tag`;
    

    (pay attention to back quotes, they are important here)

  • in table field_data_field_tag - rename column name field_tags_tid to field_tag_tid:

    ALTER TABLE `field_data_field_tag` CHANGE `field_tags_tid` `field_tag_tid` INT(10) UNSIGNED NULL DEFAULT NULL;
    

    (and yes, you have to include column specification and constraints here, otherwise it will not work)

  • do the same with field_revision_field_tags table:

    RENAME TABLE `field_revision_field_tags` TO `field_revision_field_tag`;
    ALTER TABLE `field_revision_field_tag` CHANGE `field_tags_tid` `field_tag_tid` INT(10) UNSIGNED NULL DEFAULT NULL;
    
  • remember to clear the caches after all your changes!

Also, if you have any views using any of the old fields, or any other place where the old field name could exist, you need to update them too.

In case of views for example, if you still want to update old field names directly in database (which actually could be faster than doing it from administration area, though not necessarily safer - you need to feel really confident about fiddling with database content directly), you need to update value of display_options column in views_display table.

Note that old field name could appear there several times, and also that data in this column is serialized - so if your new field name length is different from the old length, you need to update counter respectively too.

Generally, if you are using anything like phpMyAdmin, it is probably a good idea to run a search on the whole database looking for the old field name to make sure you have not missed anything.