Community Documentation

hook_schema

6 install.php hook_schema()
7 system.api.php hook_schema()
8 system.api.php hook_schema()

Define the current version of the database schema.

A Drupal schema definition is an array structure representing one or more tables and their related keys and indexes. A schema is defined by hook_schema() which must live in your module's .install file.

By implementing hook_schema() and specifying the tables your module declares, you can easily create and drop these tables on all supported database engines. You don't have to deal with the different SQL dialects for table creation and alteration of the supported database engines.

See the Schema API documentation at http://drupal.org/node/146843 for details on hook_schema(), where database tables are defined.

Return value

A schema definition structure array. For each element of the array, the key is a table name and the value is a table structure definition.

Related topics

▾ 27 functions implement hook_schema()

aggregator_schema in modules/aggregator/aggregator.install
Implementation of hook_schema().
block_schema in modules/block/block.install
Implementation of hook_schema().
blogapi_schema in modules/blogapi/blogapi.install
Implementation of hook_schema().
book_schema in modules/book/book.install
Implementation of hook_schema().
comment_schema in modules/comment/comment.install
Implementation of hook_schema().
contact_schema in modules/contact/contact.install
Implementation of hook_schema().
dblog_schema in modules/dblog/dblog.install
Implementation of hook_schema().
drupal_get_schema in includes/common.inc
Get the schema definition of a table, or the whole database schema.
drupal_install_schema in includes/common.inc
Create all tables that a module defines in its hook_schema().
drupal_uninstall_schema in includes/common.inc
Remove all tables that a module defines in its hook_schema().
filter_schema in modules/filter/filter.install
Implementation of hook_schema().
forum_schema in modules/forum/forum.install
Implementation of hook_schema().
locale_schema in modules/locale/locale.install
Implementation of hook_schema().
menu_schema in modules/menu/menu.install
Implementation of hook_schema().
node_schema in modules/node/node.install
Implementation of hook_schema().
openid_schema in modules/openid/openid.install
Implementation of hook_schema().
poll_schema in modules/poll/poll.install
Implementation of hook_schema().
profile_schema in modules/profile/profile.install
Implementation of hook_schema().
search_schema in modules/search/search.install
Implementation of hook_schema().
statistics_schema in modules/statistics/statistics.install
Implementation of hook_schema().
system_schema in modules/system/system.install
Implementation of hook_schema().
taxonomy_schema in modules/taxonomy/taxonomy.install
Implementation of hook_schema().
trigger_schema in modules/trigger/trigger.install
Implementation of hook_schema().
update_schema in modules/update/update.install
Implementation of hook_schema().
upload_schema in modules/upload/upload.install
Implementation of hook_schema().
user_schema in modules/user/user.install
Implementation of hook_schema().
_drupal_initialize_schema in includes/common.inc
Fill in required default values for table definitions returned by hook_schema().

File

developer/hooks/install.php, line 140
Documentation for the installation and update system.

Code

<?php
function hook_schema() {
  $schema['node'] = array(
    // example (partial) specification for table "node"
    'description' => 'The base table for nodes.', 
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.', 
        'type' => 'serial', 
        'unsigned' => TRUE, 
        'not null' => TRUE,
      ), 
      'vid' => array(
        'description' => 'The current {node_revisions}.vid version identifier.', 
        'type' => 'int', 
        'unsigned' => TRUE, 
        'not null' => TRUE, 
        'default' => 0,
      ), 
      'type' => array(
        'description' => 'The {node_type} of this node.', 
        'type' => 'varchar', 
        'length' => 32, 
        'not null' => TRUE, 
        'default' => '',
      ), 
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.', 
        'type' => 'varchar', 
        'length' => 255, 
        'not null' => TRUE, 
        'default' => '',
      ),
    ), 
    'indexes' => array(
      'node_changed' => array('changed'), 
      'node_created' => array('created'),
    ), 
    'unique keys' => array(
      'nid_vid' => array('nid', 'vid'), 
      'vid' => array('vid'),
    ), 
    'primary key' => array('nid'),
  );
  return $schema;
}
?>

Comments

If you need to specify an index key's length

Following is an example syntax:

<?php
 
'indexes' => array('search' => array('search', 25)),
?>

Small correction on syntax

Should be:

<?php
 
'indexes' => array(
   
'search' => array(array('search', 25)),
  );
?>

Note the extra array.

loading a new schema

Load a new schema by enabling and disabling your module may not work. You will have to uninstall the module (as opposed to just disabling it) and then re-enable it in order to trigger the module's install methods where you're running the drupal_install_schema command.

Not need to uninstall

Look in the system table (at the row where name = your_module) and set schema_version to -1

Need to drop tables?

Wouldn't you need to edit the tables in code after you uninstalled, so that you could uninstall, and it would drop the correct tables, and then reinstall and have it pick up the correct ones?

Your hook_schema should

Your hook_schema should always return the updated schema. In other words: If you add a table in hook_update_N, also add it to hook_schema.

Login or register to post comments