drupal Module development - Drupal 7 Example custom_module.install file for creating a database table


Example

Can be used in conjunction with the custom form example to create a table in the drupal database for a Mailing List feature.

This example was made by creating the table directly in my development database, then created the data for hook_schema() using the Schema module.

This allows for automatic table creation during module install on staging and production sites.

custom_module.install

/**
 * Installs the database schema.
 */
function custom_module_install() {
  drupal_install_schema('mailing_list');
}

/**
 * Uninstalls the database schema.
 */
function custom_module_uninstall() {
  drupal_uninstall_schema('mailing_list');
}

/**
* Creates the tables using the schema API.
*/
function custom_module_schema() {
  $schema['mailing_list'] = array(
    'description' => 'TODO: please describe this table!',
    'fields' => array(
      'first name' => array(
        'description' => 'TODO: please describe this field!',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'last name' => array(
        'description' => 'TODO: please describe this field!',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'email' => array(
        'description' => 'TODO: please describe this field!',
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
  );
}