Hello, I have thousands of nodes of a content type, I need to change the field type from integer to decimal. I did this

Opened the sql console and wrote this sql lines:

Use the MySQL alter statment to alter the field's datatype:
ALTER TABLE field_data_field_myfield MODIFY field_myfield_value decimal(10,2) ;

Change the field definitions in Drupal:
UPDATE field_config SET type = 'number_decimal' WHERE field_name = 'field_myfield';

Then Im getting these php notices everywhere:

Notice: Undefined index: precision in number_field_schema() (line 35 of /home/servilla/public_html/servillantasgiraldo.com.co/modules/field/modules/number/number.install).
Notice: Undefined index: scale in number_field_schema() (line 36 of /home/servilla/public_html/servillantasgiraldo.com.co/modules/field/modules/number/number.install).

What did I do wrong?

Comments

ssoulless’s picture

Look, I revert changes, then I tried to update the field through a custom module like this.

/**
 * Change field_inq_description field from Integer to Decimal
 */
function field_updates_update_7100() {
  // Manual database changes.
  db_query("UPDATE {field_config} SET type = 'number_decimal' WHERE field_name = 'field_rin'");
  db_change_field('field_data_field_rin', 'field_rin_value', 'field_rin_value', array(
    'type' => 'numeric',
    'precision' => '10',
    'scale' => '2',
  ));
  db_change_field('field_revision_field_rin', 'field_rin_value', 'field_rin_value', array(
    'type' => 'numeric',
    'precision' => '10',
    'scale' => '2',
  ));
  // Clear caches.
  field_cache_clear(TRUE);
  // Apply the new field instance configuration.
}

But I see the same notices...

ssoulless’s picture

This is the final solution. Put this code in a custom module

/**
 * Change field_rin field from Integer to Decimal
 */
//Here the number 7104 is the version of the update for more information read hook_update_n() documentation
function field_updates_update_7104() {
  // Manual database changes.
  db_query("UPDATE {field_config} SET type = 'number_decimal' WHERE field_name = 'field_rin'");
  db_change_field('field_data_field_rin', 'field_rin_value', 'field_rin_value', array(
    'type' => 'numeric',
    'precision' => '10',
    'scale' => '2',
  ));
  db_change_field('field_revision_field_rin', 'field_rin_value', 'field_rin_value', array(
    'type' => 'numeric',
    'precision' => '10',
    'scale' => '2',
  ));
  
    // we just need to add precision and scale to the field config settings:
    $data = unserialize(db_query("SELECT data FROM {field_config} WHERE field_name = 'field_rin'")->fetchField());
    $data['settings']['precision'] = 10;
    $data['settings']['scale'] = 2;

    db_update('field_config')
    ->fields(array('data' => serialize($data)))
    ->condition('field_name', 'field_rin')
    ->execute();
  
  // Clear caches.
  field_cache_clear(TRUE);
}