3

Is it possible to add multiple tables in hook_schema() while creating a custom module? I have created a custom module with the help of Drupal entities. Will it be possible to store two different table in the modulename.install file?

3 Answers 3

7

You definitely can! In your hook_schema implementation, your first array key is the table name. You can add as many as tables in this array:

Ex:

function quotes_schema(){
  $schema = array();
  $schema['quotes'] = array(
    'description' => 'Base table for Quotes. Contains all submitted data from initial page.',
    'primary key' => array('qid'),
    'fields' => array(
      ...
     ), 
  $schema['quotes_access'] = array(
    'description' => 'Access tokens to quotes.',
    'primary key' => array('qid'),
    'fields' => array(
      ...
     ),     

    ...
  return $schema;
}

This will create both quotes and quotes_keys tables.

2
  • if i would like to add two tables and both are inter related then ?? Commented Mar 26, 2013 at 11:50
  • They don't have to relate to other tables. Just make sure each table is a key in the $schema array.
    – AKS
    Commented Mar 26, 2013 at 11:55
3

Or you just create your tables in phpmyadmin or whatever GUI or terminal you prefer and let Schema module do the rest , give it a try it will save you ton of time.

1
  • +1 I think it's good practice to know how hook_schema() works, but once you get it, you should definitely move along to Schema module, it will save loads of time when you're developing and trying stuff out. Commented Mar 20, 2013 at 21:37
0

There is no limit to the number of table schemas defined in an implementation of hook_schema(). It is enough to see how many tables the System module defines in system_schema().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.