Add new comment

Adds a new field to a table in Drupal7

db_add_field($table, $field, $spec, $keys_new = array())

function my_module_schema_alter(&$schema) {
  $schema['existing_table']['fields']['new_field'] = array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
    'default' => 0,
    'description' => 'Field added by my_module',
  );
}

function my_module_install() {
  $schema = drupal_get_schema('existing_table');
  db_add_field('existing_table', 'new_field', $schema['fields']['new_field']);
}

Don't forget to call db_drop_field() in your hook_uninstall.
 

Tags

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
8 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.