0

I have an SQL CREATE TABLE.

CREATE TABLE myid_template_1
(
template_id int NOT NULL,
template_description character varying NOT NULL,

);

I want to know its equivalent query structure in Drupal 7 to create tables at run time. I tried the code below, which it didn't return any error. I wonder if it successfully creates a table, since I could not find the location of the table I created.

function myid_create_db_fields(){   
    $template_id = 1;
    $schema['myid_template_'. $template_id] = array(
        'description' => t ('Contains a particular templates data inputted from the MyID Inputting System'),
        'fields' => array(
           'template_id' => array(
               'type' => 'int',
               'not null' => TRUE,
           ),
           'template_description' => array(
               'type' => 'character varying'
               'not null' => TRUE,
           )
        ),  
    );
    db_create_table('myid_template_' . $template_id, $schema);
}

PS: I don't want to use hook_schema() because the table is not created when the module is installed.

1

You're passing the wrong thing as the schema definition.

This is what you need:

$schema['myid_template_'. $template_id] = array(...);

db_create_table('myid_template_' . $template_id, $schema['myid_template_'. $template_id);
0

You should implement hook_schema

Code sample from the hook's explanation

function hook_schema() {
  $schema['node'] = array(
    // Example (partial) specification for table "node".
    'description' => 'The base table for nodes.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The current {node_revision}.vid version identifier.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The {node_type} of this node.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'node_changed' => array('changed'),
      'node_created' => array('created'),
    ),
    'unique keys' => array(
      'nid_vid' => array('nid', 'vid'),
      'vid' => array('vid'),
    ),
    // For documentation purposes only; foreign keys are not created in the
    // database.
    'foreign keys' => array(
      'node_revision' => array(
        'table' => 'node_revision',
        'columns' => array('vid' => 'vid'),
      ),
      'node_author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
    'primary key' => array('nid'),
  );
  return $schema;
}
  • I don't want to use hook_schema because the table must not be created at installation. The table and fields in it was created and designed by the user in run time. – user46175 Feb 7 '16 at 8:52
  • You don't have to use hook_schema in .install file. – Kartagis Feb 7 '16 at 9:09
  • Really it's not necessary to use hook_schema in .install file? – user46175 Feb 7 '16 at 9:14
  • 1
    hook_schema MUST be in .install file. – Елин Й. Feb 7 '16 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy