0

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' => 'description pour la table post',
    'fields' => array(
      'pid' => array(
        'description' => 'post id',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'title',
        'type' => 'varchar',
        'not null' => TRUE,
      ),
      'body' => array(
        'description' => 'body',
        'type' => 'varchar',
        'not null' => TRUE,
      ),
      'created' => array(
              'description' => 'created',
              'not null' => TRUE,
              'mysql_type' => 'timestamp',
            ),
      'primary key' => array('pid'),
    ),
  );
}
4
  • It probably won't be an issue, but I would prefix my custom tables with my custom module's name. – sonfd Jul 20 '20 at 11:48
  • for example brother ? – David montera Jul 20 '20 at 12:08
  • 1
    For example, use $schema['mymodule_post'] so your table is mymodule_post. – sonfd Jul 20 '20 at 12:15
  • thank you it works , but from where this error cam from ==> SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version f or the right syntax to use near 'DEFAULT NULL COMMENT 'title', body VARCHAR DEFAULT NULL COMMENT 'body', `cre' at line 3 – David montera Jul 20 '20 at 12:28
1

I see two problems with your hook_schema implementation.

  1. You are missing a return statement at the end of your function to return the schema.
  2. 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' => 'description pour la table post',
    'fields' => [
      'pid' => [
        'description' => 'post id',
        'type' => 'int',
        'not null' => TRUE,
      ],
      'title' => [
        'description' => 'title',
        'type' => 'varchar',
        'not null' => TRUE,
      ],
      'body' => [
        'description' => 'body',
        'type' => 'varchar',
        'not null' => TRUE,
      ],
      'created' => [
        'description' => 'created',
        'not null' => TRUE,
        'mysql_type' => 'timestamp',
      ],
    ],
    // primary_key needs to be outside the fields array.
    'primary key' => ['pid'],
  ];
  // Don't forget to return the schema.
  return $schema;
}
1
  • 2
    I would also add that hook_install() and hook_uninstall() aren't necessary to create and delete tables. – kiamlaluno Jul 20 '20 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.