AnswerBun.com

How do I create a table when a module is installed?

Drupal Answers Asked by David montera on October 26, 2021

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'),
    ),
  );
}

One Answer

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;
}

Answered by Ajay Reddy on October 26, 2021

Add your own answers!

Related Questions

File field upload in modal/dialog

1  Asked on December 4, 2020 by ctrnz

     

How to pre-select items in a select with chosen

1  Asked on December 3, 2020 by jav

 

Update existing node (not created by Migrate) via Migrate 8.5.x

1  Asked on December 3, 2020 by vaibhav-rana

 

Secondary Tabs and views

1  Asked on December 2, 2020 by timwhelan

 

alter Suggestion box for apache solr autosearch

2  Asked on December 1, 2020 by anamika

   

User specific permission to add links to a menu

2  Asked on November 26, 2020 by mavicc

 

How to alter sort handler to keep existing order intact in views

1  Asked on November 18, 2020 by hiranya-sarma

   

Issue using Config Split on content types

0  Asked on November 18, 2020 by vecta

   

How to create simple confirmation page?

3  Asked on November 11, 2020 by mathewdragon

 

How do I add a template suggestion for a Views block?

2  Asked on October 21, 2020 by wbeasley

       

How to programmatically manage taxonomies?

1  Asked on October 15, 2020 by deny-dias

   

CSS-tags added using Display suite on products disappears

1  Asked on October 4, 2020 by anders-wallenquist

 

I have issue with commerce_order_load_multiple function

1  Asked on October 1, 2020 by user3686276

 

Why can’t I install even if I have the required PHP version?

0  Asked on September 13, 2020 by hendrik-kaiser

 

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP