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.
- Add new comment
- 221 views
Comment
In Drupal 8
Submitted by TS (not verified) on Thu, 08/20/2020 - 21:20
In Drupal 8
$spec = array(
'type' => 'varchar',
'description' => "New Col",
'length' => 20,
'not null' => FALSE,
);
$schema = Database::getConnection()->schema();
$schema->addField('existing_table', 'newcol', $spec);