Sunday, February 9, 2014

Drupal 7: Changing Field Types for a Content Type When Data Exists

Sometimes you may need to change the field type for a content type when there is already data for that content type. But through Drupal's interface it won't let you do it because data already exists for a particular content type. I figured out that you can do this by changing some things in the tables using phpMyAdmin.

I will not go into detail on what phpMyAdmin is or how to access it on your server. So this may be advanced for you. You should probably read up on what phpMyAdmin is and how to use it. Knowing some basic SQL and understanding on database structure will get you started.

For those that are comfortable using phpMyAdmin, login to it and locate the table called field_config in your drupal schema. When you find field_conifig, click on it, then click the browse tab.

Look in the "field_name" column for the name of the field that you want to change the datatype for.

Once you find your field, click the edit icon, it looks like a pencil.

In the "field" column your will see a row called "type". This is where you need to make the change. The last column called "value" needs to be changed to the new type you want.

Some possible values are list_text, datetime, text, number_float, list_boolean, image, text_long. There are so others and may depend on what modules you have installed.

Note, you may need to change the "module" column as well. Again, you need to have some understand of what data types are. So possible values are list, text, number, date, image, etc...

Set it to your new field type and click the "Go" button.

If you are converting from character or varchar to a number or int you will need to make another change in another table. If you do, locate a table called field_data_MY_FIELD_NAME table where MY_FIELD_NAME is the drupal name of the field you are changing. Here you can change the field_MY_FIELD_NAME_value type to the data type you need.

Once you do all that. Go to your content type in Drupal admin and make any other changes that may need to be done. Some times after making changes to the database you will need to change the widget type.