Skip to content

Instantly share code, notes, and snippets.

@bfodeke
Created December 13, 2017 01:34
Show Gist options
  • Save bfodeke/0a47926d5a7cf7b55a07c3f691afc67c to your computer and use it in GitHub Desktop.
Save bfodeke/0a47926d5a7cf7b55a07c3f691afc67c to your computer and use it in GitHub Desktop.
Drupal 7: Change field type in the database
<?php
// Manually update the field type in the database to `list_text`.
db_update('field_config')
->fields([
'type' => 'list_text',
])
->condition('field_name', 'field_some_field_name', '=')
->execute();
// Update the field type from 'int' to 'varchar' and update length.
db_change_field('field_data_field_some_field_name', 'field_some_field_name_value', 'field_some_field_name_value', array(
'type' => 'varchar',
'length' => '255',
));
db_change_field('field_revision_field_some_field_name', 'field_some_field_name_value', 'field_some_field_name_value', array(
'type' => 'varchar',
'length' => '255',
));
// Update values in the database for this field since we've updated the
// field type.
$num_updated_yes = db_update('field_data_field_some_field_name')
->fields([
'field_some_field_name_value' => 'Yes',
])
->condition('field_some_field_name_value', '1', '=')
->execute();
$num_updated_no = db_update('field_data_field_some_field_name')
->fields([
'field_some_field_name_value' => 'No',
])
->condition('field_some_field_name_value', '0', '=')
->execute();
// Clear caches.
field_cache_clear(TRUE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment