Drupal 6 module install file not creating tables in database

有些话、适合烂在心里 提交于 2019-12-04 09:09:47

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

if you want to add module install after the module creation you need to remove the record from system table in your drupal db, and then enable it again.

disable your module and save

goto 'system' table and find your module there

remove that record

enable your module and save

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().

I would like to share with you my experiences with this error on Drupal 6. In my first-ever module I had three tables. I had an entry for each in my hook_schema (called education_schema):

function education_schema()
{
  $schema['education_course'] = array( /* ... */ );
  $schema['education_market'] = array( /* ... */ );
  $schema['education_event'] = array( /* ... */ );
}

In my hook_install, I initially had the following:

function education_install()
{
  drupal_install_schema('education_course');
  drupal_install_schema('education_market');
  drupal_install_schema('education_event');
}

No tables were creating at module install. Why? I had no idea: no errors to be seen anywhere in the logs. Eventually I found out about the PHP extension xdebug, which, when used in education_install revealed that drupal_install_schema failed because it could not find the routines education_course_schema, education_course_market and education_course_event. At that point the solution was quite obvious:

function education_install()
{
  drupal_install_schema('education');
}

And voila, it worked!

So, I learned that drupal_install_schema does not log any error when it fails, that only one call to drupal_install_schema is required and that it installs all schemas that you return in the array, that the API documentation of drupal_install_schema is worth reading, and finally that xdebug is a very handy utility!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!