Drupal 7 – Updating a node type after existing data

Aug 11, 2016 | John Cornelius, Web Development

In recent coding adventures I was tasked with changing the storage and output display of a particular database field for one of our Drupal 7 clients.  The column in question I discovered was in fact a field associated with a content type group.  If your new to Drupal, content types are used to store contextual data by means of fields in a grouped fashion.  Content types provide data scalability by allowing multiple fields that can have behaviors, and even permissions assigned to them.  You can set the type of data to be stored and even determine how its displayed (Checkbox, input, select, etc…) to the end user right from the interface.

The Issue


However, one caveat of this type of scalable data structure is introduced when you try to update a field within a content type.  Let me digress by saying if you have yet to enter any data for your content type, then you can skip this article all together, but something tells me if you are reading this, then you have experienced the caveat i mentioned, being you can not change a fields storage type once data has been inserted.

Fair enough, i can see how this would break things and even lose data, switching a fields storage type at will, but if it has to be done we will have to leave the comfort of Drupal’s gui to achieve it! If you have never opened up the data storage hood of Drupal and you have created quite a few content types, you will be taken back at first at the amount of tables involved in achieving the this type of data storage. This is because each field within a content type requires its own table to store what content bundle it belongs to and of course all the data inserted.

The Solution


In my case i needed to change an integer column called “log_weight” to a decimal.  To achieve this,  I had to alter 2 tables.   Start with “field_config”, which contains all content type fields, defining the storage and front end render type.  Now i likely could have used phpMyAdmin or some either database gui to make these updates, but i did not want to take the chance of missing out whatever internal queries drupal may run to log such actions. Here is the first query i ran directly in the themes template.php.

<?php
db_query("UPDATE DATABASE_NAME.field_config SET type = 'number_decimal' WHERE field_config.field_name = 'field_log_weight'");
?>

node type

The last table I needed to update was “field_data_field_log_weight”.

<?php
db_query("ALTER TABLE field_data_field_log_weight CHANGE field_log_weight_value field_log_weight_value DECIMAL(12,1) NULL DEFAULT NULL");
?>

node type

Once these queries were ran i could enter decimal values with ease! If your using views dont forget to update the field in that particular view to allow decimal rendering and specify scale.

Share This