Created
March 9, 2023 05:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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