Drupal 6模块安装文件没有在数据库中创建表

时间:2010-08-16 19:21:21

标签: drupal drupal-modules

我正在使用Schema API在Drupa 6.17上为我的模块创建表,但这些表并没有在数据库中创建。我安装了Schema模块,它告诉我,虽然我的模块的模式被识别,但它的表不在数据库中。它出现在失踪:

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

以下是我的test.install文件的内容。

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

4 个答案:

答案 0 :(得分:10)

如果要在创建模块后添加模块安装,则需要从drupal db中的系统表中删除记录,然后再次启用它。

禁用您的模块并保存

转到'system'表并在那里找到你的模块

删除该记录

启用您的模块并保存

答案 1 :(得分:4)

编辑:

这是我刚才写的代码。例如:

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

答案 2 :(得分:3)

第一次启用模块时,Drupal只运行一次模块的hook_install()。除非您通过并禁用,卸载,然后重新启用该模块,否则将再次调用模块的hook_install()。

如果您已经创建了模块的发行版并且想要将模式添加到现有安装中,那么您将需要添加一个调用db_create_table()的hook_update_N()实现。

答案 3 :(得分:1)

我想与您分享我在Drupal 6上遇到此错误的经历。在我的第一个模块中,我有三张桌子。我的hook_schema(称为education_schema)中有一个条目:

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

在我的hook_install中,我最初有以下内容:

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

在模块安装时没有创建表。为什么?我不知道:日志中的任何地方都没有错误。最后我发现了PHP扩展xdebug,当education_install中使用时,drupal_install_schema失败了,因为它无法找到例程education_course_schemaeducation_course_marketeducation_course_event。那时解决方案非常明显:

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

瞧,它有效!

所以,我了解到drupal_install_schema在失败时没有记录任何错误,只需要调用drupal_install_schema,并且它会安装你在数组中返回的所有模式,即API drupal_install_schema的文档值得一读,最后xdebug是一个非常方便的实用程序!

相关问题