1

In my module I am using hook_schema() to create an empty (custom) DB table when the module is installed. However, there are some use cases where the module would need to be installed when the table already exists. This gives the following error:

Drupal\Core\Database\SchemaObjectExistsException: Table example_table already exists. in Drupal\Core\Database\Schema->createTable() (line 594 of [PATH]\core\lib\Drupal\Core\Database\Schema.php).

It looks like the module is still installed, but the error likely means that future install logic might not be executed.

Is it possible to perform the schema logic only if the table does not exist, or some other way to ignore the table if it exists?

3
  • 1
    However, there are some use cases where the module would need to be installed when the table already exists. why is it not cleaned up on uninstall?
    – Kevin
    May 17, 2018 at 14:51
  • It is. That's not the use case, there are others. For example, if someone wants to import this table before installing the module, or restore it after the module is uninstalled for future reference (both seem possible based on use cases I have seen).
    – Ynhockey
    May 17, 2018 at 14:56
  • hook_schema exists so you can say "this table belongs to my module, Drupal will manage it for me". The docs back that up: The tables declared by this hook will be automatically created when the module is installed, and removed when the module is uninstalled.. The schema API can't be used for tables that Drupal doesn't manage (or at least not without changing how it works at a more fundamental level). I suspect you'll be out of luck here
    – Clive
    May 17, 2018 at 15:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.