Drupal 7: Create an id with auto increment by hook_schema

Change the int type to serial.

The below is an example:

function info_manager_schema() {
$schema = array();
$schema[‘info_manager’] = array(
// specification for  table info_manager
‘fields’ => array(
‘id’ => array(
‘description’ => t(‘The primary identifier for a record.’),
‘type’ => ‘serial’,
‘unsigned’ => TRUE,
‘not null’ => TRUE,),
‘first_name’ => array(
‘description’ => t(‘The first name of the contact.’),
‘type’ => ‘varchar’,
‘length’ => 255,),
‘last_name’ => array(
‘description’ => t(‘The last name of the contact.’),
‘type’ => ‘varchar’,
‘length’ => 255,),
’email’ => array(
‘description’ => t(‘The email of the contact.’),
‘type’ => ‘varchar’,
‘length’ => 255,),
),
‘primary key’ => array(‘id’),
);
return $schema;
}

Leave a Reply

Your email address will not be published. Required fields are marked *