drupal-7drupal-hooksdrupal-schema

Drupal 7 hook_schema not installing database table


Any help would be brilliant.

function request_gold_pack_schema() {
    $schema['request_gold_pack_customer_details'] = array(
        'description' => 'Table to store all customer details.',
        'fields' => array(     
            'rid' => array(
                'type' => 'int',  
                'not null' => TRUE, 
                'default' => 0,
                'auto increment' => TRUE
            ),
            'title' => array(
                'type' => 'varchar', 
                'length' => 10,
                'not null' => TRUE,
                'default' => ''
            ),
            'first_name' => array(
                'type' => 'varchar',
                'length' => 50, 
                'not null' => TRUE,
                'default' => ''
            ),
            'last_name' => array(
                'type' => 'varchar',
                'length' => 50,
                'not null' => TRUE,
                'default' => ''
            ),
            'house_name_no' => array(
                'type' => 'varchar', 
                'length' => 50,
                'not null' => TRUE,
                'default' => ''
            ),
            'street' => array(
                'type' => 'varchar',
                'length' => 160,
                'not null' => TRUE,
                'default' => ''
            ),
            'town' => array(
                'type' => 'varchar',
                'length' => 50,
                'not null' => TRUE, 
                'default' => ''
            ),
            'county' => array(
                'type' => 'varchar', 
                'length' => 50,
                'not null' => TRUE,
                'default' => ''
            ),
            'telephone' => array(
                'type' => 'int',
                'length' => 12,
                'not null' => TRUE,
                'default' => ''
            ),
            'email' => array(
                'type' => 'varchar', 
                'length' => 255,
                'not null' => TRUE,
                'default' => ''
            ),
            'date_registered' => array(
                'mysql_type' => 'DATETIME',
                'not null' => TRUE
            ),
            'primary' => array(
                'rid'
            )
        )
    );

    return $schema;
}

which gives me the following errors

Notice: Undefined index: type in DatabaseSchema_mysql->processField() (line 205 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc). Notice: Undefined index: :normal in DatabaseSchema_mysql->processField() (line 205 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/mysql/schema.inc). PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to stor' at line 13: CREATE TABLE {request_gold_pack_customer_details} ( rid INT NOT NULL DEFAULT 0, title VARCHAR(10) NOT NULL DEFAULT '', first_name VARCHAR(50) NOT NULL DEFAULT '', last_name VARCHAR(50) NOT NULL DEFAULT '', house_name_no VARCHAR(50) NOT NULL DEFAULT '', street VARCHAR(160) NOT NULL DEFAULT '', town VARCHAR(50) NOT NULL DEFAULT '', county VARCHAR(50) NOT NULL DEFAULT '', telephone INT NOT NULL DEFAULT '', email VARCHAR(255) NOT NULL DEFAULT '', date_registered DATETIME NOT NULL, primary DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Table to store all customer details.'; Array ( ) in db_create_table() (line 2688 of /Users/richardskinner/Sites/www.goldrushmoney.com-local/httpdocs/includes/database/database.inc).

Been trying to find a solution for hours.

Thanks.


Solution

  • It is an issue with your primary key. You hve it listed in the fields array (it should not be) and it should be referenced as 'primary key' and not 'primary', like so:

    function request_gold_pack_schema(){
        $schema['request_gold_pack_customer_details'] = array(
            'description' => 'Table to store all customer details.',
            'fields' => array(     
                  // Your field definitions
             ),
             'primary key' => array(
                 'rid'
             )
        );
        return $schema;
    }
    

    Check out the schema api documentation on drupal.org.

    I would also recommend setting the rid field to type serial and leaving off the auto increment parameter (Drupal will handle that). So the 'rid' field definitions would be as such:

    'rid' => array(
        'type' => 'serial',  
        'unsigned' => TRUE, 
        'not null' => TRUE, 
    )