Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. database.api.php

function hook_schema

Define the current version of the database schema.

Only procedural implementations are supported for this hook.

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.

The tables declared by this hook will be automatically created when the module is installed, 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 https://www.drupal.org/node/146843 for details on schema definition structures. Note that foreign key definitions are for documentation purposes only; foreign keys are not created in the database, nor are they enforced by Drupal.

Return value

array 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

Schema API
API to handle database schemas.
Hooks
Define functions that alter the behavior of Drupal core.
14 functions implement hook_schema()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

ban_schema in core/modules/ban/ban.install
Implements hook_schema().
comment_schema in core/modules/comment/comment.install
Implements hook_schema().
dblog_schema in core/modules/dblog/dblog.install
Implements hook_schema().
file_schema in core/modules/file/file.install
Implements hook_schema().
help_schema in core/modules/help/help.install
Implements hook_schema().

... See full list

2 invocations of hook_schema()
ModuleInstaller::installSchema in core/lib/Drupal/Core/Extension/ModuleInstaller.php
Creates all tables defined in a module's hook_schema().
ModuleInstaller::uninstallSchema in core/lib/Drupal/Core/Extension/ModuleInstaller.php
Removes all tables defined in a module's hook_schema().

File

core/lib/Drupal/Core/Database/database.api.php, line 580

Code

function hook_schema() : array {
    $schema['users_data'] = [
        'description' => 'Stores module data as key/value pairs per user.',
        'fields' => [
            'uid' => [
                'description' => 'The {users}.uid this record affects.',
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ],
            'module' => [
                'description' => 'The name of the module declaring the variable.',
                'type' => 'varchar_ascii',
                'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
                'not null' => TRUE,
                'default' => '',
            ],
            'name' => [
                'description' => 'The identifier of the data.',
                'type' => 'varchar_ascii',
                'length' => 128,
                'not null' => TRUE,
                'default' => '',
            ],
            'value' => [
                'description' => 'The value.',
                'type' => 'blob',
                'not null' => FALSE,
                'size' => 'big',
            ],
            'serialized' => [
                'description' => 'Whether value is serialized.',
                'type' => 'int',
                'size' => 'tiny',
                'unsigned' => TRUE,
                'default' => 0,
            ],
        ],
        'primary key' => [
            'uid',
            'module',
            'name',
        ],
        'indexes' => [
            'module' => [
                'module',
            ],
            'name' => [
                'name',
            ],
        ],
        // For documentation purposes only; foreign keys are not created in the
        // database.
'foreign keys' => [
            'data_user' => [
                'table' => 'users',
                'columns' => [
                    'uid' => 'uid',
                ],
            ],
        ],
    ];
    return $schema;
}
RSS feed
Powered by Drupal