2010-08-16 44 views
4

我正在使用Schema API在Drupa 6.17上爲我的模塊創建表,但表不會在數據庫中創建。我安裝了Schema模塊,它告訴我雖然我的模塊模式被識別,但它的表不在數據庫中。它出現在Missing下:Drupal 6模塊安裝文件不在數據庫中創建表

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

回答

3

編輯:

這裏是代碼,我只是寫的作品。以下爲例:

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

我剛試過,仍然得到相同的結果。 – George 2010-08-16 19:38:23

+0

你是否禁用了模塊,卸載(會出錯),然後重新啓用它? – Kevin 2010-08-16 19:43:34

+0

沒有等待,你說得對,我混了起來。安裝/卸載看起來不錯。 Schema模塊的用途是什麼? – Kevin 2010-08-16 19:48:00

3

Drupal只在第一次啓用模塊時運行模塊的hook_install()一次。除非你通過並禁用,卸載,然後重新啓用模塊,否則模塊的hook_install()會再次被調用。

如果您已經創建了模塊版本,並且想要將模式添加到現有安裝中,您將需要添加調用db_create_table()的hook_update_N()實現。

9

如果您想要在模塊創建後添加模塊安裝,您需要從drupal數據庫的系統表中刪除記錄,然後重新啓用它。

禁用模塊並保存

轉到「系統」表,發現有

你的模塊刪除記錄

啓用模塊並保存

+0

這是唯一對我有用的東西。但是現在,當我禁用模塊時,表格仍然保留在數據庫中。好吧。 :) - EDIT:剛剛瞭解了「卸載」部分,以便涵蓋它! – Favio 2011-06-03 05:51:20

+0

如果使用devel,則可以在www.yoursite.com/devel/reinstall中重新安裝模塊 – mjimcua 2015-01-09 10:48:32

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_schema,education_course_marketeducation_course_event。在這一點上,解決方案非常明顯:

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

瞧,它的工作!

所以,我瞭解到,當它失敗drupal_install_schema不記錄任何錯誤,只有一個呼叫drupal_install_schema是必需的,它安裝您在數組中返回所有的模式,即中drupal_install_schema API文檔是值得一讀,最後這個xdebug是一個非常方便的工具!