Community Documentation

drupal_install_schema

6 common.inc drupal_install_schema($module)
7 common.inc drupal_install_schema($module)
8 common.inc drupal_install_schema($module)

Creates all tables defined in a module's hook_schema().

Note: This function does not pass the module's schema through hook_schema_alter(). The module's tables will be created exactly as the module defines them.

Parameters

$module: The module for which the tables will be created.

Related topics

File

includes/common.inc, line 6724
Common functions that many Drupal modules will need to reference.

Code

<?php
function drupal_install_schema($module) {
  $schema = drupal_get_schema_unprocessed($module);
  _drupal_schema_initialize($schema, $module, FALSE);

  foreach ($schema as $name => $table) {
    db_create_table($name, $table);
  }
}
?>

Comments

No longer necessary in Drupal 7

As of Drupal 7, a call to drupal_install_schema() is no longer necessary. See http://drupal.org/update/modules/6/7: "A module no longer should explicitly install or uninstall its database schema in hook_install() or hook_uninstall()."

Correct link

install_schema only run on first enable

Since drupal_install_schema() is only run on the first time a module is enabled this could lead to some frustration during module development. If you have an error in your schema and enable the module your table will not be created. If you fix your schema and then re-enable your module your table will still not be created.

You can fix this by adding an empty hook_uninstall() to your module and then uninstalling your module trough the Drupal admin interface. The next time you enable your module it will be seen as "the first time".

My personal preference is to add a db_table_exists() check to hook_enable() to make sure the table is created.

<?php
/**
  * Implement hook_enable()
  */
function yourmodulename_enable() {
 
//Check if table exists, if not install the schema.
 
if(db_table_exists('table_name') == FALSE) {
   
drupal_install_schema('yourmodulename');
  }
}
?>

Cheers,
Eric

No Longer necessary in Drupal 7

Login or register to post comments