You are here

function hook_schema in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/database.api.php \hook_schema()
  2. 7 modules/system/system.api.php \hook_schema()
  3. 9 core/lib/Drupal/Core/Database/database.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.

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

23 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().
book_schema in core/modules/book/book.install
Implements hook_schema().
comment_schema in core/modules/comment/comment.install
Implements hook_schema().
database_test_schema in core/modules/system/tests/modules/database_test/database_test.install
Implements hook_schema().
dblog_schema in core/modules/dblog/dblog.install
Implements hook_schema().

... See full list

3 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().
SchemaInspector::getTablesSpecification in core/tests/Drupal/TestTools/Extension/SchemaInspector.php
Returns the module's schema specification.

File

core/lib/Drupal/Core/Database/database.api.php, line 521
Hooks related to the Database system and the Schema API.

Code

function hook_schema() {
  $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;
}