I have constructed a module that, using the .install file, creates a table on a database different than the default. The module installs fine and functions fine – just a hello world example in the controller. However, when I uninstall the module I get: “The website encountered an unexpected error. Please try again later.” and the reports log there is: DatabaseExceptionWrapper: SQLSTATE[42S02]...

Here is the .install file code:

function hello_world_schema()
    $schema['create_table'] = array(
      'fields' => array(
        'id' => array('type' => 'serial', 'not null' => TRUE,),
        'name' => array('type' => 'varchar', 'length' => 40, 'not null' => TRUE,),
        'last_name' => array('type' => 'varchar', 'length' => 40, 'not null' => TRUE,),
        'message' => array('type' => 'varchar', 'length' => 250, 'not null' => TRUE,),
        'boogie' => array('type' => 'varchar', 'length' => 20, 'not null' => TRUE,),
      'primary key' => array('id'),);
    return $schema;

I do realize that I'm tell hook_schema to switch to notTheDefaultDB and don’t switch it back – if it is switched before the return statement the table is created in the default.

As I said, the module seems to function as intended, but blows up when uninstalled.

Is there a way to write the module so it doesn’t blow up on uninstall or perhaps an entirely different way to do what I’m after?

2 Answers 2


To my mind, the entire purpose of hook_schema is to build the Drupal schema. If you need/want to create tables outside of Drupal, it seems like just using PDO in a hook_install() and hook_uninstall() implementation might be the way to go. Can you explain your use case a little bit more?

All that said, I don't see an obvious reason for your code to fail.

  • I'm guessing it's failing because the uninstall tries to uninstall a file that is not in it's default DB. My experience w/ Druapl is limited and perhaps I need to explore the hooks you mention (install / uninstall.).
    – Mike
    Feb 8, 2016 at 23:06
  • I'm sure that's why it's failing (based on the definition of SQLSTATE 42S02 (see dev.mysql.com/doc/refman/5.5/en/error-messages-server.html). What I mean,is that in looking at the source code, I don't see anything in drupal_uninstall_schema() or its dependencies that would cause it not to honor your invocation of db_set_active(). See api.drupal.org/api/drupal/includes!common.inc/function/…. Might be worth opening an issue in the issue queue, but given your somewhat unusual use case, it might not get a ton of attention. Feb 9, 2016 at 21:51

The Schema API is tailored to declare/manipulate/alter your Drupal project's schemas. Instead of using it to create and delete tables, use raw PHP with synchronous calls. No need to use Schema API when you're no manipulating your Drupal schema.

Your Answer

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

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