0

My custom module name is "person" and its .install file has the following code.

<?php
/**
 * Implementation of hook_install().
 */
function person_install() {

  drupal_install_schema('person');
}

/**
 * Implementation of hook_schema.
 */
function person_schema() {

  // Static (meta) tables.

  $schema['person'] = array(
    'fields' => array(
      'pid'            => array('type' => 'int', 'length' => 11, 'not null' => TRUE, 'default' => ''),
      'name' => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
      'city'        => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
      'country'        => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE)
    ),
    'primary key' => array('pid'),
  );
return $schema;
}

But when I installed my module the table has not been created.Any thing wrong with this code?

2
  • 1
    Did you definitely install the module and not just re-enable it? Enabling/disabling and installing/uninstalling are two separate things with Drupal modules. If you have Drush + Devel try drush dre person to be 100% sure. If not, you can do the same thing through the UI. Your code looks right, so it's probably just that the hooks aren't being invoked
    – Clive
    Nov 12, 2013 at 14:45
  • @Clive.Yes two things are different.But am sure I uninstalled and re-installed it.ok will try it out your idea.
    – Ranjani
    Nov 12, 2013 at 14:47

1 Answer 1

2

In your schema your setting pid as primary key. You are setting '' value for primary key. Remove that default value for pid and try.

Then your code should be:

<?php
/**
 * Implementation of hook_install().
 */
function person_install() {

  drupal_install_schema('person');
}

/**
 * Implementation of hook_schema.
 */
function person_schema() {

// Static (meta) tables.

$schema['person'] = array(
  'fields' => array(
    'pid'            => array('type' => 'int', 'length' => 11, 'not null' => TRUE),
    'name' => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
    'city'        => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
    'country'        => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE)
  ),
  'primary key' => array('pid'),
  );
return $schema;
}

After edit the code don't disable and enable the module, hook_install will only call at module install process, so you should first disable the module, unistall the module and then install the module again.

3
  • It created table after installation but the pid column is not set as auto increment field.Any idea?
    – Ranjani
    Nov 13, 2013 at 10:25
  • 1
    Change your primary key type attribute in to serial instead of int. Then your code shhould be: 'pid' => array('type' => 'serial', 'length' => 11, 'not null' => TRUE),
    – ARUN
    Nov 13, 2013 at 11:12
  • remove length attribute and add size attribute with primary key. Then your code should be: 'pid' => array('type' => 'serial', 'size' => 'big', 'not null' => TRUE),
    – ARUN
    Nov 13, 2013 at 11:37

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.