views:

206

answers:

2

I'm using the Schema API to create tables for my module on Drupa 6.17, but the tables just do not get created in the database. I have the Schema module installed, and it tells me that while the schema for my module is recognized, its table is not in the database. It comes up under Missing:

Tables in the schema that are not present in the database.
test
* test_table

Here are the contents for my test.install file.

<?php
// $Id$
function test_schema() {
$schema['test_table'] = array(
    'description' => t('Test table'),
    'fields' => array(
        'nid' => array(
            'description' => t('test field'),
            'type' => 'serial',
            'not null' => TRUE,
        ),
        'options' => array(
            'description' => t('other test field'),
            'type' => 'text',
            'not null' => FALSE,
        ),
    ),
    'primary key' => array('nid'),
    );
    return $schema;
}
function test_install() {
    drupal_install_schema('test');
}
function test_uninstall() {
    drupal_uninstall_schema('test');
}
+2  A: 

Edit:

Here is code I just wrote that works. Follow as example:

/**
 * Implementation of hook_schema().
 */

function action_alert_schema() {
 $schema['action_alert'] = array(
    'description' => 'Action Alert table.',
    'fields' => array(
   'aid' => array(
        'description' => 'The serial ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
   ),
      'nid' => array(
        'description' => 'The primary identifier of the node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
   ),
      'uuid' => array(
        'description' => 'The session id of the user if the UID is not present.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '0',
      ),
   ),
    'primary key' => array('aid'),
  );

return $schema;

}

/**
 * Implementation of hook_install().
 */
function action_alert_install() {
  drupal_install_schema('action_alert');
}

/**
 * Implementation of hook_uninstall().
 */
function action_alert_uninstall() {
 drupal_uninstall_schema('action_alert');
}
Kevin
I just tried it and still get the same result.
George
Did you disable the module, uninstall (will error), then reenable it?
Kevin
No wait, you're right, I got mixed up. install/uninstall look right. What is the Schema module for?
Kevin
It works now after uninstalling, but you have to use the module name in drupal_install_schema. When I uninstalled and reenabled, it still gave me the same problem, but the table got created when I used the module name as the argument. Thanks!
George
@Kevin: the Schema module parses MySQL table definitions and creates schemas for them that you can paste in to your .install files. It also checks for tables without valid schemas (eg tables you just created in MySQL without going through Drupal) among other things.
Jergason
Gotcha. Never heard of it before now.
Kevin
+1  A: 

Drupal only runs a module's hook_install() once when you first enable the module. Unless you go through and disable, uninstall, and then re-enable the module will your module's hook_install() get called ever again.

If you had already created a release of your module and are wanting to add a schema to existing installs, you will want to add an implementation of hook_update_N() that calls db_create_table().

Dave Reid

related questions