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.

This hook is called at both install and uninstall time, and in the latter case, it cannot rely on the .module file being loaded or hooks being known. If the .module file is needed, it may be loaded with drupal_load().

The tables declared by this hook will be automatically created when the module is first enabled, and removed when the module is uninstalled. This happens before hook_install() is invoked, and after hook_uninstall() is invoked, respectively.

By declaring the tables used by your module via an implementation of hook_schema(), these tables will be available 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 Handbook at http://drupal.org/node/146843 for details on schema definition structures.

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

▾ 53 functions implement hook_schema()

aggregator_schema in modules/aggregator/aggregator.install
Implements hook_schema().
block_schema in modules/block/block.install
Implements hook_schema().
book_schema in modules/book/book.install
Implements hook_schema().
comment_schema in modules/comment/comment.install
Implements hook_schema().
contact_schema in modules/contact/contact.install
Implements hook_schema().
DatabaseConnection::$schema in includes/database/database.inc
The schema object for this connection.
DatabaseConnection::schema in includes/database/database.inc
Returns a DatabaseSchema object for manipulating the schema.
DatabaseSchema in includes/database/schema.inc
DatabaseSchema::$defaultSchema in includes/database/schema.inc
Definition of prefixInfo array structure.
DatabaseSchema_sqlite::$defaultSchema in includes/database/sqlite/schema.inc
Override DatabaseSchema::$defaultSchema
DatabaseSchema_sqlite::introspectSchema in includes/database/sqlite/schema.inc
Find out the schema of a table.
database_test_schema in modules/simpletest/tests/database_test.install
Implements hook_schema().
dblog_schema in modules/dblog/dblog.install
Implements hook_schema().
drupal_get_complete_schema in includes/bootstrap.inc
Gets the whole database schema.
drupal_get_schema in includes/bootstrap.inc
Gets the schema definition of a table, or the whole database schema.
drupal_install_schema in includes/common.inc
Creates all tables defined in a module's hook_schema().
drupal_uninstall_schema in includes/common.inc
Removes all tables defined in a module's hook_schema().
field_schema in modules/field/field.install
Implements hook_schema().
field_sql_storage_schema in modules/field/modules/field_sql_storage/field_sql_storage.install
Implements hook_schema().
field_test_field_schema in modules/field/tests/field_test.install
Implements hook_field_schema().
field_test_schema in modules/field/tests/field_test.install
Implements hook_schema().
file_field_schema in modules/file/file.install
Implements hook_field_schema().
filter_schema in modules/filter/filter.install
Implements hook_schema().
forum_schema in modules/forum/forum.install
Implements hook_schema().
hook_field_schema in modules/field/field.api.php
Define the Field API schema for a field structure.
image_field_schema in modules/image/image.install
Implements hook_field_schema().
image_schema in modules/image/image.install
Implements hook_schema().
list_field_schema in modules/field/modules/list/list.install
Implements hook_field_schema().
locale_schema in modules/locale/locale.install
Implements hook_schema().
menu_schema in modules/menu/menu.install
Implements hook_schema().
module_test_schema in modules/simpletest/tests/module_test.install
Implements hook_schema().
node_access_test_schema in modules/node/tests/node_access_test.install
Implements hook_schema().
node_schema in modules/node/node.install
Implements hook_schema().
number_field_schema in modules/field/modules/number/number.install
Implements hook_field_schema().
openid_schema in modules/openid/openid.install
Implements hook_schema().
poll_schema in modules/poll/poll.install
Implements hook_schema().
profile_schema in modules/profile/profile.install
Implements hook_schema().
rdf_schema in modules/rdf/rdf.install
Implements hook_schema().
SchemaTestCase::testSchema in modules/simpletest/tests/schema.test
search_schema in modules/search/search.install
Implements hook_schema().
shortcut_schema in modules/shortcut/shortcut.install
Implements hook_schema().
simpletest_schema in modules/simpletest/simpletest.install
Implements hook_schema().
statistics_schema in modules/statistics/statistics.install
Implements hook_schema().
system_schema in modules/system/system.install
Implements hook_schema().
taxonomy_field_schema in modules/taxonomy/taxonomy.install
Implements hook_field_schema().
taxonomy_schema in modules/taxonomy/taxonomy.install
Implements hook_schema().
taxonomy_test_schema in modules/simpletest/tests/taxonomy_test.install
Implements hook_schema().
text_field_schema in modules/field/modules/text/text.install
Implements hook_field_schema().
tracker_schema in modules/tracker/tracker.install
Implements hook_schema().
trigger_schema in modules/trigger/trigger.install
Implements hook_schema().
update_schema in modules/update/update.install
Implements hook_schema().
user_schema in modules/user/user.install
Implements hook_schema().
_field_sql_storage_schema in modules/field/modules/field_sql_storage/field_sql_storage.module
Return the database schema for a field. This may contain one or more tables. Each table will contain the columns relevant for the specified field. Leave the $field's 'columns' and 'indexes' keys empty to get only the base schema.

File

modules/system/system.api.php, line 3027
Hooks provided by Drupal core and the System module.

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_revision}.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'),
    ), 
    'foreign keys' => array(
      'node_revision' => array(
        'table' => 'node_revision', 
        'columns' => array('vid' => 'vid'),
      ), 
      'node_author' => array(
        'table' => 'users', 
        'columns' => array('uid' => 'uid'),
      ),
    ), 
    'primary key' => array('nid'),
  );
  return $schema;
}
?>

Comments

Column description not stored in MySQL column comment

The description attribute of a column should be stored as a MySQL column comment.

See this post for more info.

It is not clear what this attribute is used for, in any case.

No need to call drupal_install_schema as of Drupal 7

Database table is automatically created based on schema during module installation so no need to call drupal_install_schema() from hook_install() any more.

See here http://drupal.org/node/224333#install-schema

hook_schema_alter()

It is somehow not mentioned anywhere here, but it exists: http://api.drupal.org/hook_schema_alter

Can I define the database for

Can I define the database for this schema? If I want to store the schema to other database than defaule.

Using multiple databases

Login or register to post comments