[drupal] hook_field_schema is not called at all! What happened?

Hi, I’m using this piece of code to create a field named zoho_task_id in every node of the support_ticket type.


  if (!field_info_field($field_name)) {
      //drupal_set_message ("creating field");
      //die();
      // Create the field.
      $field = array(
        'field_name' => $field_name,
        'type' => 'field_hidden_text',
        'settings' => array(
          'max_length' => 255,
        ),
        'cardinality' => 1,
      );
      field_create_field($field);

      // Create the instance.
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => 'node',
        'bundle' => 'support_ticket',
        'label' => 'Zoho Task ID',
        'description' => 'Zoho Task ID',
        'required' => FALSE,
      );

      field_create_instance($instance);
      watchdog('zoho', t('!field_name was added successfully.', array('!field_name' => $field_name)));
      //drupal_set_message ("created field " . $field_name);
  }
  else {
    watchdog('zoho', t('!field_name already exists.', array('!field_name' => $field_name)));
  }

Now I want to override the hook_field_schema to add index and unique key into the field.


function zoho_field_schema($field)
{
    //Config::display_test_result_as_drupal_message($field);
    echo "Calling hook_field_schema....";
    watchdog('zoho', "Calling hook_field_schema....");
    watchdog('zoho', t('Creating !field_name....', array('!field_name' => $field['field_name'])));

    if($field['field_name'] == Config::$zoho_task_id_fieldname)
    {
      watchdog('zoho', t('Modifying !field_name.', array('!field_name' => $field['field_name'])));
      return array(
        'columns' => array(
          'value' => array(
            'type' => 'varchar',
            'size' => '255',
            'not null' => FALSE,
          ),
        ),
        'indexes' => array(
          'value' => array('value'),
        ),
        'unique keys' => array(
          'value' => array('value'),
        ),
      );
    }
    elseif($field['field_name'] == Config::$zoho_comment_id_fieldname)
    {
      watchdog('zoho', t('Modifying !field_name.', array('!field_name' => $field['field_name'])));
      return array(
        'columns' => array(
          'value' => array(
            'type' => 'varchar',
            'size' => '255',
            'not null' => FALSE,
          ),
        ),
        'indexes' => array(
          'value' => array('value'),
        ),
        'unique keys' => array(
          'value' => array('value'),
        ),
      );
    }
}


As you can see, I used the watchdog function to log when the function is called. But when I checked the log, there is no messageCalling hook_field_schema” in there. And when I check the data table field_data_zoho_task_id, I see there is no unique key nor index in the zoho_task_id_value column. I guesss these mean the hook_field_schema wasn’t called. I wonder how can I get it work? I put the function zoho_field_schema in the .install file as instructed in https://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_schema/7 but nothing happened. Please help me with this problem. Thanks.

You will need to use an update hook to alter the schema once the module has been installed. The alternative is to completely uninstall the module and reinstall though that may lead to other problems depending on how the module is built.

zoho/zoho.install


...

function zoho_update_7001() {
   ...
}

If you go this route than you will need to update the module through update.php.

Drupal Docs:
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_update_N/7

I used both of them (update & uninstall) but none of them worked. I checked the log and I see the watchdog lines in the hook field_schema weren’t called. How can I invoke the hook after the field is created?

Just defining the functions means nothing. You need to navigate to /update.php and go through the update process for the module(s) that require updates.