I am trying to display an already existing table to Drupal Views. This table exists within my current Drupal Database, along with my Drupal tables.

The table's name is ACCOUNT_PNL and I would like to expose the fields in this table to Drupal Views.

I've followed the instructions here, but when I go and enable this module, I get the following error from my site:

DatabaseSchemaObjectExistsException: Table <em class="placeholder">ACCOUNT_PNL</em> already exists. in DatabaseSchema->createTable() (line 657 of /var/www/drupal7/includes/database/schema.inc).

I understand that the table already exists, but isn't the tutorial code supposed to only "expose" it to Views so I can then use Views to display it?

Perhaps there is an error in my understanding, any help would be appreciated.


The tutorial you linked to implements hook_schema() which means Drupal will want to take care of the table implementation for you.

I don't know this for definite but I think Views requires that the table be under Drupal's control when you're exposing custom data.

I don't know of a way to tell the installer to ignore the schema installation, so the simplest thing to do would probably be to rename the custom table temporarily, install the module (allowing Drupal to create the table), then import the data from the temp table into the newly created one.

If you then uninstall the module (not just disable), your custom table and the data will be lost so it's worth keeping a backup if you think that's likely to happen.

| improve this answer | |
  • That all seems very complicated. Would you know how I can give my tables(s) to be under the control of Drupal? Or to simply just let Views display the rows in my table? – chlong Jun 12 '12 at 17:30
  • 2
    I guess you could go into the system table and manually set the status of your module to 1...that would make Drupal think the module's already been installed. You'd need to clear the caches after doing that – Clive Jun 12 '12 at 18:06
  • 2
    If the table already exists in Drupal wouldn't you use hook_schema_alter rather than hook_schema to modify the DB? – Citricguy Aug 13 '12 at 5:20
  • Depending on the usecase you might want to remove hook_schema completely. You are using hook_views_data in mymodule.views.inc so the database structure is already known to Views. That also makes sure your table won't accidentally be cleared if you would disable the module. This however only applies if you write this for your specific website. When building a module for more people you'd want to create the table for them. – Neograph734 Feb 23 '14 at 19:44

The tables declared by hook_schema() hook will be automatically created when the module is first enabled, and removed when the module is uninstalled.

As there is already table in your Drupal database, so you are getting the error that table already exist when you install the module.

There is no need of implements hook_schema() function in your installation file, if you have already table in your database with record.

Or you can implements hook_schema_alter() in your module to alter your existing table.

| improve this answer | |

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.