.install does not create table in database

3 replies [Last post]
logicspk
Offline
Last seen: 10 years 4 weeks ago
Joined: 2011-12-02

I am coming from Oracle so do not have problem in understanding database and SQL. I am new to Drupal and php so consider that I am mssing something in the process. I have successfully installed 'schema' module and its dependencies. I have tried to modify the files given in 'Build a module' resource pack (Trails module) and created following 'simple' files to test creation of table named 'department'. I am using Drupal 7.9 on Windows 7 with MySQL.(WAMP)

I would like to submit here that I am not interested in creation of menus or blocks etc. at this time, I am just interested as to how can I create a table through .install file.

I shall be thankful if someone can show me a simple example that creates a table in MySQL. Created files with their codes are shown below:

1 college.info file

; $Id$
name = College
description = Educational module
package = Educational
core = 7.x
configure = admin/config/college
files[] = college.module

2 college.install file
/**
* Implements hook_install()
*/
function college_install() {
}

/**
* Implements hook_uninstall().
*/
function college_uninstall() {
}

function college_schema()
{
$schema['department'] = array(
'description' => 'TODO: please describe this table!',
'fields' => array(
'dept_id' => array(
'description' => 'TODO: please describe this field!',
'type' => 'int',
'not null' => TRUE,
),
),
);
return $schema;
}

3 college.module file
<?php

/**
* Implements hook_menu().
*/
function college_menu() {
// Module settings.
$items['admin/config/college'] = array(
'title' => 'College',
'description' => 'college configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('college_admin_settings'),
'file' => 'college.admin.inc',
'file path' => drupal_get_path('module', 'college'),
);
return $items;
}

4 college.admin.inc file

<?php
// $Id$

function college_admin_settings() {
$form['college_block_max'] = array(
'#type' => 'select',
'#title' => t('Maximum number of items to display'),
'#options' => drupal_map_assoc(range(1, 200)),
'#default_value' => variable_get('college_block_max', ''),
'#description' => t('This will set the maximum allowable number that can be displayed in a history block'),
'#required' => TRUE,
);
return system_settings_form($form);
}

I shall be thankful

regards

logicspk
Offline
Last seen: 10 years 4 weeks ago
Joined: 2011-12-02
Problem solved but another question

Chris, I struggled with clearing up cache, restarting all process of WAMP, closing the browser and then restarting again. NO Luck.

Second time in addition to the above actions, I removed the module folder. Pasted the folder module again and renamed the module name (with another name) in all the files.

It worked. Table was created.

Now the question?
Is the table created only once i.e. the first time the module is enabled (I mean when check box on the left side of the module is checked) and configuration is saved.

If yes then, is there any way that it gets created everytime the module is enabled?

regards

Chris Shattuck
Offline
Last seen: 10 weeks 4 days ago
Joined: 2009-08-16
Hi there, Good job on

Hi there,

Good job on figuring this out! To answer your question, the hook_install() function, which is found in the .install file, runs every time the module is installed. If it has been disabled and then uninstalled, the module will run the hook_install() function again. If the table has not been removed via a hook_uninstall() function, then I believe it will simply throw an error, rather then remove and then add the table again.

If you or anyone else are already using the module, then you can use a hook_update_N() function (see video below) to add the table in.

Let me know if that answers your question.

Cheers!
Chris

logicspk
Offline
Last seen: 10 years 4 weeks ago
Joined: 2011-12-02
hook_uninstall worked

Thanks Chris
Since I was just interested in creation of table, I ignored hook_uninstall() and did not write hook_uninstall() in the .install file. I observed that even when I disabled the module, I had no way to uninstall the module. Therefor whenever I re-enabled the module table creation had no effect (If I removed the table manually it was not re-created, if table was already existing there was not error .... so not effect at all).

After writing the hook_uninstall, I could see the module name in listing of 'unistall' tab in the 'modules' menu. Uninstalled it. Enabled again. Table created.

thanks for your time

regards

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.