![](/img/trans.png)
[英]Drupal 8.6 Commerce: in .install file without module_uninstall() remove tables from database. Why?
[英]Drupal 6 module install file not creating tables in database
我正在使用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');
}
如果要在創建模塊后添加模塊安裝,則需要從drupal db中的系統表中刪除該記錄,然后再次啟用它。
禁用您的模塊並保存
轉到'system'表並在那里找到你的模塊
刪除該記錄
啟用您的模塊並保存
編輯:
這是我剛剛編寫的代碼。 舉個例子:
/**
* 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');
}
第一次啟用模塊時,Drupal只運行一次模塊的hook_install()。 除非您通過並禁用,卸載,然后重新啟用該模塊,否則將再次調用模塊的hook_install()。
如果您已經創建了模塊的發行版並且想要將模式添加到現有安裝中,那么您將需要添加一個調用db_create_table()的hook_update_N()實現。
我想與大家分享我在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_market
和education_course_event
。 那時解決方案非常明顯:
function education_install()
{
drupal_install_schema('education');
}
瞧,它有效!
所以,我了解到drupal_install_schema
在失敗時沒有記錄任何錯誤,只需要調用drupal_install_schema
,並且它會安裝你在數組中返回的所有模式, drupal_install_schema
的API文檔值得閱讀,最后是xdebug是一個非常方便的實用程序!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.