I am writing a module for which I have to add a field to my database. So I need to use db_add_filed function, So in which file I should use this function? I mean should it be in .module file or .inc file?

2 Answers 2


I think you should write your code for adding new field in DB in your .module file. Here is the code for you :

function xyz_schema_alter(&$schema) {
  // Add field to existing schema.
  $schema['node']['fields']['show_in_inbox'] = array(
    'type' => 'int',
    'length' => '4',
    'default' => '1',
    'not null' => TRUE,
    'unsigned' => TRUE,  
    'description' => 'Field added by xyz module',

function xyz_enable(){
    if (!db_field_exists('node', 'show_in_inbox')) {
        $schema = drupal_get_schema('node');
        db_add_field('node', 'show_in_inbox', $schema['fields']['show_in_inbox']);

The new field will be added in your existing database on enabling this module.

  • Thank you so much Astha chauhan, I added my filed to comment table, do you know which schema should I use now to show my new added filed in comment form page? Jun 8, 2014 at 20:26

If the field is static, I should think you want to create any db tables as part of the install (https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_install/7). Just don't forget to remove it as part of an uninstall.

If it's being created dynamically, then the point at which it needs to be created should dictate where and when to place the code.

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.