3

I'm working the SchemaAPI but can't get it to create the tables. This is the content of my schema_test.install which is mostly copied form the examples modules.

<?php
function schema_test_install() {
  drupal_install_schema('schema_test');
}
function schema_test_schema() {
  $schema['schema_test_one'] = array(
    'description' => 'Stores example person entries for demonstration purposes.',
    'fields' => array(
      'pid'  => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique person ID.',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => "Creator user's {users}.uid",
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Name of the person.',
      ),
      'surname' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Surname of the person.',
      ),
      'age' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The age of the person in years.',
      )
    ),
    'primary key' => array('pid'),
    'indexes' => array(
      'name'    => array('name'),
      'surname' => array('surname'),
      'age'     => array('age'),
    ),
  );
  return $schema;
}
/* vim: set ts=8 sw=2 tw=79 cc=80 ft=php:*/

I also have the Schema Module enabled and it lists this schema under "Missing". So it's recognized but never installed when the module is being enabled. What am I missing here?

8

I got caught out in Drupal 7 by the fact that you don't need to call drupal_install_schema. Also, hook_install should now be used to perform actions on your tables, rather than set them up.

Drupal 7 sees your schema and installs it automatically when your module is activated, so because you're calling it manually, it's trying to install the schema twice.

This might help explain it a bit.

Also, try checking in your database to see if the tables already exist. If it's installed incorrectly, Drupal might be seeing the tables and thinking they're correct when actually they're not.

| improve this answer | |
3

I Found out that in Drupal 7, you need to manually uninstall the module to create/recreate the tables defined by your schema.

To do this, first disable the module, then click the "uninstall" tab on the Modules page. Then Uninstall your module and reenable it. This should run the install hook.

| improve this answer | |

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