| 7 system.api.php | hook_schema() |
| 6 install.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 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
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- 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().
- drupal_get_complete_schema in includes/
bootstrap.inc - Gets the whole database schema.
- drupal_get_schema_unprocessed in includes/
common.inc - Returns the unprocessed and unaltered version of a module's schema.
File
- modules/
system/ system.api.php, line 3121 - Hooks provided by Drupal core and the System module.
Code
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
PermalinkThe 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.
Fixed
PermalinkI'm pretty sure this now happens. In any case, this is not the place for bug reports...
No need to call drupal_install_schema as of Drupal 7
PermalinkDatabase 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()
PermalinkIt is somehow not mentioned anywhere here, but it exists: http://api.drupal.org/hook_schema_alter
Can I define the database for
PermalinkCan I define the database for this schema? If I want to store the schema to other database than defaule.
Using multiple databases
PermalinkTake a look at this: http://api.drupal.org/api/drupal/includes--database--database.inc/functi...
Unable to create your table?
PermalinkI have found that if you update hook_schema() while your module is enabled there seems to be an issue getting Drupal to generate your tables even after disabling and enabling your module. To get around this:
1. disable the module
2. uninstall the module (via the modules page)
3. enable the module
Your updated schema should now be reflected in the database. Hope this helps someone because I was scratching my head for a while at this.
Thanks for this message. I
PermalinkThanks for this message. I too was scratching my head as to why it wouldn't install my db schema and this solved the issue.
Merely disabling a module is
PermalinkMerely disabling a module is not a destructive action, as (unless it was coded improperly) it will not delete the database tables associated with the module, nor will it delete any site-wide variables associated with it. This is so that you can disable a module, then enable it, and still have all your data intact. Having to completely uninstall a module in order to nuke its tables is proper behavior.
If you're a developer wanting to test that your schema works correctly, you may find it handy to use Drush to disable, uninstall, then reinstall your module in one go:
drush dis my_module -y; drush pm-uninstall my_module -y; drush en my_module -yEven quicker if you have
PermalinkEven quicker if you have devel installed you can use the devel-reinstall (dre for short) command.
drush dre my_module -yThis command is fantastic!
PermalinkThis command is fantastic!
Tables not removed on uninstall
PermalinkFor me the tables I define get created just fine on install, but uninstalling doesn't remove any of them. I have this experience consistently, whenever I use hook_schema. The 'fix' for me is doing schema_uninstall('mymodule') in hook_uninstall, but this still seems pretty ugly. Am I doing something wrong or is this a bug/design error?Disregard that.
Can you mention how did you solve this?
PermalinkI am facing exactly the same issue. Table gets created on install but does not get removed while uninstalling. I'm using hook_schema to serve table details.Oops! Figured out myself. Just in case anyone is wondering how to do from Drupal admin - Go to admin/modules/uninstall to completely uninstall the module after disabling it.