How do I create a table when a module is installed? I want to add a custom table when a module I develop is installed. The code I am using doesn't seem to work, as the post table isn't created.
What is wrong with the code I am using?
/**
 * Installs the database schema.
 */
function mymodule_install() {
  drupal_install_schema('post');
}

/**
 * Uninstalls the database schema.
 */
function mymodule_uninstall() {
  drupal_uninstall_schema('post');
}

/**
* Creates the tables using the schema API.
*/
function mymodule_schema() {
  $schema['post'] = array(
    'description' =&gt; 'description pour la table post',
    'fields' =&gt; array(
      'pid' =&gt; array(
        'description' =&gt; 'post id',
        'type' =&gt; 'int',
        'not null' =&gt; TRUE,
      ),
      'title' =&gt; array(
        'description' =&gt; 'title',
        'type' =&gt; 'varchar',
        'not null' =&gt; TRUE,
      ),
      'body' =&gt; array(
        'description' =&gt; 'body',
        'type' =&gt; 'varchar',
        'not null' =&gt; TRUE,
      ),
      'created' =&gt; array(
              'description' =&gt; 'created',
              'not null' =&gt; TRUE,
              'mysql_type' =&gt; 'timestamp',
            ),
      'primary key' =&gt; array('pid'),
    ),
  );
}
I see two problems with your hook_schema implementation.

You are missing a return statement at the end of your function to return the schema.
primary_key needs to be outside the fields array on the same level as the fields and the description.


/**
 * Implements hook_schema().
 */
function MYMODULE_schema() {
  $schema['post'] = [
    'description' =&gt; 'description pour la table post',
    'fields' =&gt; [
      'pid' =&gt; [
        'description' =&gt; 'post id',
        'type' =&gt; 'int',
        'not null' =&gt; TRUE,
      ],
      'title' =&gt; [
        'description' =&gt; 'title',
        'type' =&gt; 'varchar',
        'not null' =&gt; TRUE,
      ],
      'body' =&gt; [
        'description' =&gt; 'body',
        'type' =&gt; 'varchar',
        'not null' =&gt; TRUE,
      ],
      'created' =&gt; [
        'description' =&gt; 'created',
        'not null' =&gt; TRUE,
        'mysql_type' =&gt; 'timestamp',
      ],
    ],
    // primary_key needs to be outside the fields array.
    'primary key' =&gt; ['pid'],
  ];
  // Don't forget to return the schema.
  return $schema;
}