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');
}
Edit:
Here is code I just wrote that works. Follow as example:
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
(callededucation_schema
):In my
hook_install
, I initially had the following: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 thatdrupal_install_schema
failed because it could not find the routineseducation_course_schema
,education_course_market
andeducation_course_event
. At that point the solution was quite obvious:And voila, it worked!
So, I learned that
drupal_install_schema
does not log any error when it fails, that only one call todrupal_install_schema
is required and that it installs all schemas that you return in the array, that the API documentation ofdrupal_install_schema
is worth reading, and finally that xdebug is a very handy utility!