2

I want to create database table for my custom module.

Inside mymodule.install I have:

function mymodule_schema(){
    $schema['a_visits'] = array(

    'description' => 'visits table',
    'fields' => array(
        'id' => array(
            'description' => 'make id primary key auto increment',
            'type' => 'serial',
            'not null' => TRUE,
        ),
        'oferta' => array(
            'description' => 'nazwa oferty',
            'type' => 'varchar',
            'length' => '255',
            'not null' => TRUE,
        )
    ),
    'primary key' => array('id'),
    );
    return $schema;
}

function mymodule_install() {
   drupal_install_schema('a_visits');
}


function mymodule_uninstall() {
   drupal_uninstall_schema('a_visits');
}

Then I've moved the module's file to server under sites/all/modules/custom/mymodule and enabled it via admin panel. Unfortunately no table in database has been created (the module is listed as active now).

1
  • 1
    You don't have to explicitly install the schema anymore: api.drupal.org/comment/3073#comment-3073 . However, i ran your code and the table was created in my database. Can you disable, uninstall and then enable the module? Also, any notices/warnings in the dblog?
    – OPTASY
    Nov 23, 2016 at 13:33

2 Answers 2

4

hook_schema alone is sufficient to create database tables. I assume you are just disabling and then enabling the module which will not help you to create tables in the database. To get it working, admin/modules' locate your module and disable it. Then navigate to 'admin/modules/uninstall' and again locate your module and uninstall it. After this, go toadmin/modules', and enable your module. Now you can find the respective tables in your database.

You can also use drush for the same. drush dis module_name, then drush pm-uninstall module_name and finally drush en module_name.

Also, as per @OPTASY Canada, do check your dblog for any warning or error.

1
  • 2
    drush dre module_name also disables, uninstalls and renables it with one command.
    – sareed
    Nov 23, 2016 at 14:28
2

Only this much will work. make sure you first uninstall and reinstall this module again.

function mymodule_schema(){
    $schema['a_visits'] = array(

    'description' => 'visits table',
    'fields' => array(
        'id' => array(
            'description' => 'make id primary key auto increment',
            'type' => 'serial',
            'not null' => TRUE,
        ),
        'oferta' => array(
            'description' => 'nazwa oferty',
            'type' => 'varchar',
            'length' => '255',
            'not null' => TRUE,
        )
    ),
    'primary key' => array('id'),
    );
    return $schema;
}

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.