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

发布于 2024-09-14 06:07:40 字数 939 浏览 2 评论 0原文

我正在使用 Schema API 在 Drupa 6.17 上为我的模块创建表,但这些表并未在数据库中创建。我安装了架构模块,它告诉我,虽然我的模块的架构被识别,但它的表不在数据库中。它出现在“缺失”下:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

混浊又暗下来 2024-09-21 06:07:40

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

禁用您的模块并保存

转到“系统”表并在那里找到您的模块

删除该记录

启用您的模块并保存

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

叶落知秋 2024-09-21 06:07:40

编辑:

这是我刚刚编写的有效代码。如下示例:

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

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');
}
哆啦不做梦 2024-09-21 06:07:40

当您第一次启用模块时,Drupal 仅运行该模块的 hook_install() 一次。除非您执行并禁用、卸载,然后重新启用该模块,否则您的模块的 hook_install() 会再次被调用。

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

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

提赋 2024-09-21 06:07:40

我想与您分享我在 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 中使用时显示 < code>drupal_install_schema 失败,因为找不到例程 education_course_schemaeducation_course_marketeducation_course_event。那时解决方案非常明显:

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

瞧,它成功了!

因此,我了解到,drupal_install_schema 在失败时不会记录任何错误,只需要对 drupal_install_schema 进行一次调用,并且它会安装您在数组中返回的所有模式, drupal_install_schema 值得一读,最后,xdebug 是一个非常方便的实用程序!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文