Adding a table with hook_schema ()

I plan to add a table using hook_schema. I defined the circuit and call it in my module. However, no schema is created. Where am I going wrong. I have the following code in sample2.install file

function sample2_schema(){ $schema['mytable1'] = array( 'description' => t('My table description'), 'fields' => array( 'mycolumn1' => array( 'description' => t('My unique identifier'), 'type' => 'serial', 'unsigned' => true, 'not null' => true, ), 'myvarchar' => array( 'description' => t('My varchar'), 'type' => 'varchar', 'length' => 32, 'not null' => true, ), 'mytimestamp' => array( 'description' => t('My timestamp'), 'type' => 'int', 'not null' => true, ), ), 'indexes' => array( 'myvarchar' => array('myvarchar'), ), 'primary key' => array('mycolumn1'), 'unique keys' => array( 'mycolumn1' => array('mycolumn1'), ), ); return $schema; } function sample2_install(){ drupal_install_schema('sample2'); } function sample2_uninstall(){ drupal_uninstall_schema('sample2'); } 

To call it, I use the following code in the sample2.module file.

  $record = (object) NULL; $record->myvarchar = 'blah'; $record->mytimestamp = strtotime('now'); drupal_write_record('mytable1',$record); 

This does not create any table.

+4
source share
3 answers

If the sample2 module existed and was enabled before adding the functions sample2_schema() and sample2_install() , you need to remove it (and not just disable it), and then install and enable it again. Otherwise, Drupal will not use your hook_install() . Another solution is to set the schema in the hook_update_N() implementation. Then use u pdate.php or drush updatedb to run the update code.

 function sample2_update_6001() { drupal_install_schema('sample2'); } 
+6
source

To add a new table to an existing db module schema, you can do this:

  /** * Add generic API transaction log table. */ function commerce_paydollar_update_7004(&$sandbox){ // Get schema definition for this module $schema = commerce_paydollar_schema(); // Create a selected table from the schema definition db_create_table('commerce_paydollar_api_txn', $schema['commerce_paydollar_api_txn']); $sandbox['#finished'] = TRUE; } 
+1
source

See another question that is very similar, Drupal 6 module installation file, not creating tables in the database .

In the second part of your question, you are talking about a table "call". Before trying to write something to a table from a module, I would suggest connecting to your database and checking if the table was successfully created first.

Removing / reinstalling your module will cause the creation of a circuit in the installation file. If the syntax is correct, your table will be created.

If you find that your table is not being created, and you think that this is a syntax problem, I would suggest simplifying your code by first trying to add only one column, then the remaining columns and the latest indexes. You can install / uninstall each of these steps.

0
source

Source: https://habr.com/ru/post/1333612/


All Articles