8.5.x schema.inc drupal_install_schema($module)
8.0.x schema.inc drupal_install_schema($module)
8.1.x schema.inc drupal_install_schema($module)
8.2.x schema.inc drupal_install_schema($module)
8.3.x schema.inc drupal_install_schema($module)
8.4.x schema.inc drupal_install_schema($module)
8.6.x schema.inc drupal_install_schema($module)
6.x common.inc drupal_install_schema($module)
7.x 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

2 calls to drupal_install_schema()
module_enable in includes/module.inc
Enables or installs a given list of modules.
system_install in modules/system/system.install
Implements hook_install().

File

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

Code

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

ywarnier’s picture

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

ericmulder1980’s picture

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.

/**
  * 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

skruf’s picture

humblecat’s picture

I tried install all my tables in one module saying "Schema Profile" without any other operations. I have put all my schema definitions in hook_schems(), and have hook_install(), hook_uninstall(), and hook_enable() all created, but drupal will install NOTHING into the database.

crobinson’s picture

I think the comment above was unclear - it makes it sound like this should never be done in D7. That's the part that's not true. It just shouldn't be done in hook_install() because Drupal now does it by default. For module authors who didn't have a schema in an earlier release and are creating one via a hook_update_N(), this is still a good way to create the initial schema.

New Zeal’s picture

There is one small problem with using drupal_install_schema if it has already been run once for a module. Let's say you want to add a new table in an update, then because the drupal_install_schema function has already been processed for that module, nothing happens. The only way to ensure your new table is created is to use db_create_table() directly.

epiphanydigital’s picture

function modulename_update_001() {
  drupal_install_schema('modulename');
}
JesseDP’s picture

Sometimes I just add one new table to my module.
I do it this way:

<?php
function my_module_update_7001() {
  $schema = module_invoke('my_module', 'schema');
  db_create_table('table_name', $schema['table_name']);
}
?>
bulldozer2003’s picture

This is simple and sweet, thank you!

Piyushchandra07’s picture

 function test_module_update_007(){
db_create_table('my_table',$schema['my_table']);
}