Drupal 6 db_create_table cannot handle primary/unique keys

The Drupal API provides a function called db_create_table which allows you to create a table in your database. If you need to add a table for a module, after the module is installed, then you could use this function in your update hook.

The documentation of db_create_table says that it accepts three arguments, an array which will contain the query result, the name of the table and an array containing the schema definition of the table. The schema definition has to be in the structure defined by the Drupal Schema API.

What I recently noticed was, that db_create_table does not seem to accept primary/unique keys in the schema definition. It seems like you need to first create a table without any constraints. For this, you'll have to provide db_create_table with an array containing the schema without any key constraints. After doing that, you can use db_add_primary_key in order to create a primary key. Similarly, there is a function to create a unique key constraint - db_add_unique_key.

The Drupal documentation itself does not seem to say much on this, but I tested this on Drupal 6, haven't really tried it on Drupal 7 yet.

Comments

Popular posts from this blog

Errors while trying to monitor Asterisk through Nagios

Configuring remote access for couchdb

Proxy Error 502 “Reason: Error reading from remote server” with Apache 2.2.3