6

I want to add an extra field to the users table from a custom module. I am not sure how exactly can it be done. I'm sure it is done in mymodule.install file.
Should I use db_add_field, or is there any other way? And where should I implement it? hook_schema (but the schema is already defined), or hook_install?

3 Answers 3

5

For the sake of giving a (hopefully) complete answer:

  • You will need to alter the schema of an existing module, signalling the change to the system. So implement hook_schema_alter().
  • You will need to implement hook_install() to actually add the field to the database.
  • You will need to implement hook_uninstall() as well, to remove the extra field when the module is uninstalled. You might need to do extra cleanup at this stage.

An example which alters the Webform module's webform_submitted_data table:

function MYMODULE_schema_alter(&$schema) {
  $schema['webform_submitted_data']['fields']['extra'] = array(
    'description' => 'Some random extra info.',
    'type' => 'text',
    'size' => 'big',
  );
}

function MYMODULE_install() {
  db_add_field('webform_submitted_data', 'extra', array(
    'description' => 'Some random extra info.',
    'type' => 'text',
    'size' => 'big',
  ));
}

function MYMODULE_uninstall() {
  db_drop_field('webform_submitted_data', 'extra');
}
1
  • this is creating the column correctly in the table. But its not saving the value to the table. any help?
    – Rick
    Commented Dec 1, 2020 at 18:50
3

Maybe, I went too early to put this as question, but I've figured it out on my local drupal installation. Just added the following code to MYMODULE.install file. Used hook_install() and db_add_field().

function MYMODULE_install() {
  db_add_field('users', 'encuid', array(
    'type' => 'varchar', 
    'length' => 60,
    'not null' => TRUE, 
    'default' => 0,
  ));
}
5
  • 2
    The more bullet-proof way would be to use hook_schema_alter(), that way all parts of the system (views, drupal_write_record(), etc) will play nicely with the new column
    – Clive
    Commented Aug 22, 2012 at 10:50
  • @Clive thank you for the suggestion. I've implemented hook_schema_alter(), but the column was not created. Do I need to call it from hook_install(), or something else?
    – AjitS
    Commented Aug 22, 2012 at 11:06
  • You should disable and UNINSTALL the module after the changes. Then enable it again. Commented Aug 22, 2012 at 12:37
  • 2
    Sorry about that I wasn't very clear...you should implement that hook as well as physically adding the column to the table with db_add_field()
    – Clive
    Commented Aug 22, 2012 at 16:27
  • drupal.stackexchange.com/questions/298493/… - Can someone check this question?
    – Rick
    Commented Dec 1, 2020 at 18:49
3

I think that best method for not loose data is create a hook_update in your custom module and make a drush updb.


/**
 * Implements hook_update_7084().
 * Add new field in custom table.
 */
function my_module_update_7084() {
  db_add_field('mytable', 'my_new_field', array(
    'type' => 'varchar', 
    'length' => 24,
    'not null' => TRUE, 
    'default' => 'favorite',
  ));
}

Regards... ;) Miguel

1
  • 2
    Prefer updating the schema changes in hook_schema and hook_update_n (both the hooks). Post that, drush updb will update the schema changes.
    – AkiShankar
    Commented Oct 13, 2016 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.