I am new to Drupal module development. I am posting in Drupal forums first time for my first I am building a module to store links of Some social networking sites and thumbnails of those sites. I want to create couple of tables in database for which i am using drupal_install_scheme() function. But when i enable the module, no tables are generated in database.
Please help me with this.
Thanks in advance!!!

This is my code :----

// $Id$
/**
* @file
* Module for managing links of social networking sites.
*/


/**
* Implementation of hook_install().
*/
function social_site_install() {
// Use schema API to create database table.
drupal_install_schema('social_site');
}



/**
* Implementation of hook_uninstall().
*/
function social_site_uninstall() {
// Remove tables.
drupal_uninstall_schema('social_site');
}



/**
* Implementation of hook_schema().
*/
function social_site_schema() {
$schema['social_site_type'] = array(
'description' => t('Stores site types of the social_site module.'),
'fields' => array(
      'tid' => array(
        'description' => 'The primary identifier for a type.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'type' => array(
        'description' => 'The machine-readable name of this type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE),
      'name' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'description'    => array(
      'description' => 'A brief description of this type.',
      'type' => 'text',
      'not null' => TRUE,
      'size' => 'medium'),
    ),
    'primary key' => array('type'),
    ),

$schema['social_site_links'] = array(
'description' => t('Stores sites\' links according to each user.'),
 'fields' => array(
    'sid' => array(
      'description' => 'Primary identifier for site link.',
      'type' => 'serial',
      'unsigned' => TRUE,
      'not null' => TRUE),
    'uid' => array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
      'description' => t('The {user}.uid of the user crating the link.')
    ),
    'timestamp' => array(
     'description' => 'A Unix timestamp indicating when this site link was created.',
     'type' => 'int',
     'not null' => TRUE,
     'default' => 0),
    'url' => array(
      'description' => 'The url of the desired site.',
      'type' => 'text',
      'not null' => TRUE,
      'size' => 'medium'),
      'tid' => array(
        'description' => 'The {social_site_type}.tid of the type which this link belongs to.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'thumbnail' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
    ),

    'primary key' => array('url'),
);

return $schema;
}

Thanks!!
Is there anything else I'll have to do for creating tables than this code??

Comments

nevets’s picture

Assuming you have social_site.info, social_site.module and the above is social_site.install it should work IF installing for the first time. If you had previously installed the module, you will need to disable and un-install the re-install/enable.

sushyl’s picture

Thanks Steve!
Yes I have .info and .module of social_site. I have written this code in .module file, i have studied several modules, and did like the other modules like node module and some other modules. I disabled and tried to uninstall module but could not find Uninstall option in uninstall tab of modules page.
NOw I tried above code with on completely new instance of Drupal, with i social_site.install file but no tables were created. =(

I think there must be minor mistake in code. Don't know. But module development is very interesting.
Thanks once again!!

-
Cheers!
Sushil H

rahulbile’s picture

hey Sushil.

I have written this code in .module file

you have to write this code in .install file.

Cheers

sushyl’s picture

I tried writing this code in .install file in a new instance in drupal, i have mentioned above. :)
Do you think the code is correct??

-
Cheers!
Sushil H

rahulbile’s picture

yes sushil,
The code looks fine.

Cheers

jaypan’s picture

The tables won't install on module enable, they will only install on first-time install. First, disable the module, then click the 'uninstall' tab, select the module, and uninstall it (note: if your module doesn't have a hook_uninstall() function, it won't appear here - make sure you have added this function). Then, click the list tab, and re-enable your module. This is a first-time install, and the tables will install.

Either that or use the devel module, enable the development block and then use the 'reinstall modules' link in the block.

Contact me to contract me for D7 -> D10/11 migrations.

sushyl’s picture

Thanks jay! I'll try this. But i removed the record for my "social_site" module in drupal's System table. i think it does the same thing.

-
Cheers!
Sushil H

jaypan’s picture

It does, but it's not good to directly edit the database like that.

Contact me to contract me for D7 -> D10/11 migrations.

sushyl’s picture

It worked!!
I wrote new(similar) code for new instance and it worked.
I learned how to do it. I could not track the problem but All i wanted was to learn how to do this thing.
Thank you very much.
:)

-
Cheers!
Sushil H

sushyl’s picture

Here is the problem-
There were 3 mistakes in this code, i have listed and shown using comments
1. I have put a comma after last element of the array.
2. I read in some code that we can not give default value to a text and varchar attributes in mysql.
3. serial data Type can be assigned to only one attribute in a table.(we use this type for auto-increment).
Thank you all for your help. :)

/**
* Implementation of hook_schema().
*/
function social_site_schema() {
$schema['social_site_type'] = array(
'description' => t('Stores site types of the social_site module.'),
'fields' => array(
      'tid' => array(
        'description' => 'The primary identifier for a type.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'type' => array(
        'description' => 'The machine-readable name of this type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE),
      'name' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),   // default value for varchar
      'description'    => array(
      'description' => 'A brief description of this type.',
      'type' => 'text',
      'not null' => TRUE,
      'size' => 'medium'), //this is last element of array
    ),
    'primary key' => array('type'), //this is last element of array
    ),

$schema['social_site_links'] = array(
'description' => t('Stores sites\' links according to each user.'),
'fields' => array(
    'sid' => array(
      'description' => 'Primary identifier for site link.',
      'type' => 'serial',   //type = 'serial' 1st time
      'unsigned' => TRUE,
      'not null' => TRUE),
    'uid' => array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
      'description' => t('The {user}.uid of the user crating the link.')
    ),
    'timestamp' => array(
     'description' => 'A Unix timestamp indicating when this site link was created.',
     'type' => 'int',
     'not null' => TRUE,
     'default' => 0),
    'url' => array(
      'description' => 'The url of the desired site.',
      'type' => 'text',
      'not null' => TRUE,
      'size' => 'medium'),
      'tid' => array(
        'description' => 'The {social_site_type}.tid of the type which this link belongs to.',
        'type' => 'serial',     //type = 'serial' 2nd time
        'unsigned' => TRUE,
        'not null' => TRUE),
      'thumbnail' => array(
        'description' => 'The human-readable name of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''), //this is last element of array
    ),

    'primary key' => array('url'), //this is last element of array
);

return $schema;
}

-
Cheers!
Sushil H

jaypan’s picture

1. I have put a comma after last element of the array.

This wasn't an error. PHP\Drupal accepts commas after the last element in an array with no issues.

Contact me to contract me for D7 -> D10/11 migrations.

sushyl’s picture

Thanks, i didn't know that.
I made all these changes, listed the things i did and then checked, and i got the tables installed.

-
Cheers!
Sushil H

drupalam’s picture

It's not working for me no matter what. the install file never gets read. I can put errors in that file, remove from system table, move files, then reinstall, reenable.

NOTHING. the install file isn't even read. function drupal_install_schema returns NOTHING.

If the install file was read, then there would be an error because I purposely put bad php in there. I have no idea how to even debug this.

jaypan’s picture

What is the name of your module, and what is the name of your install file?

Contact me to contract me for D7 -> D10/11 migrations.