2

I try to install my module, and everything goes ok, without an error while enabling

DatabaseSchemaObjectExistsException: Table products already exists. in DatabaseSchema->createTable()...

I checked it and there wasn't the products table.

My install function is:

function products_schema() {
    $schema['products'] = array(
        'description' => 'The base table for products.',
        'fields' => array(
            'pid' => array(
                'description' => 'The primary identifier for a product.',
                'type' => 'serial',
                'unsigned' => TRUE,
                'not null' => TRUE,
            ),
            'name' => array(
                'description' => 'The title of this product, always treated a non-markup plain text.',
                'type' => 'varchar',
                'length' => 255,
                'not null' => TRUE,
                'default' => '',
            ),
            'created' => array(
                'description' => 'The Unix timestamp when the product was created.',
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
            ),
            'changed' => array(
                'description' => 'The Unix timestamp when the product was most recently saved.',
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
            ),
            'description' => array(
                'description' => 'The product description.',
                'type' => 'text',
                'not null' => FALSE,
            ),
        ),
        'indexes' => array(
            'node_changed' => array('changed'),
            'node_created' => array('created'),
        ),
        'primary key' => array('pid'),
    );

    return $schema;
}

function products_install() {
    drupal_install_schema('products');

    cache_clear_all(NULL, NULL, '*');
}

function products_uninstall() {
    drupal_uninstall_schema('products');

    $result = db_select('file_usage', 'f')
            ->fields('f')
            ->condition('type', 'product')
            ->condition('module', 'products')
            ->execute()
            ->fetchAll();

    foreach ($result as $row) {
        $file = file_load($row->fid);

        if ($file) {
            file_usage_delete($file, 'products', NULL, NULL, 0);
            file_delete($file);
        }
    }
}
2

From your previous questions it looks like you're using Drupal 7, but the above code is partly for Drupal 6.

You don't need to add/remove the schema manually any more in Drupal 7; implementing hook_schema() is enough. Drupal installs the schema for you, and then when you it hits your implementation hook_install(), the table does already exists so it throws an error.

Just remove the calls to drupal_install_schema() and drupal_uninstall_schema(), uninstall the module and try again - it should work fine.

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.