Skip to content

Instantly share code, notes, and snippets.

@prashantdsala
Created March 9, 2023 05:16
Show Gist options
  • Save prashantdsala/7c250eaf99d247a0b07f449384169f24 to your computer and use it in GitHub Desktop.
Save prashantdsala/7c250eaf99d247a0b07f449384169f24 to your computer and use it in GitHub Desktop.
How to create a custom table using hook_schema() from .install file in Drupal
<?php
/**
* @file
* Install, update and uninstall functions for the modulename module.
*/
/**
* Implements hook_schema().
*/
function modulename_schema() {
// Table name 'custom_install_example'.
$schema['custom_install_example'] = [
'description' => 'Table description.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique record ID.',
],
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The {users}.uid of the user who created the record.',
],
'status' => [
'description' => 'Boolean indicating whether this record is active.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
],
'type' => [
'type' => 'varchar_ascii',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Type of the record.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp when the record was created.',
],
'data' => [
'type' => 'blob',
'not null' => TRUE,
'size' => 'big',
'description' => 'The arbitrary data for the item.',
],
],
'primary key' => ['id'],
'indexes' => [
'type' => ['type'],
'uid' => ['uid'],
'status' => ['status'],
],
];
return $schema;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment