1

I am creating a simple module. In that module I need to save the values in a table and I have created an .install file to create the table when the module is installed.

While installing the module, it is not creating the table and it gives a PDOException Error. I am unable to find the issue with my code.

The error message:

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 'unsigned NOT NULL COMMENT ' AMOUNT', rate unsigned NOT NULL COMMENT 'R' at line 3: CREATE TABLE {sampletest} ( id INT unsigned NOT NULL COMMENT 'My unique identifier', amount unsigned NOT NULL COMMENT 'AMOUNT', rate unsigned NOT NULL COMMENT ' RATE', time unsigned NOT NULL COMMENT ' TIME', PRIMARY KEY (id) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT ' table'; Array ( ) in db_create_table() (line 2717 of H:\xampp\htdocs\drupal7\includes\database\database.inc) ;

Content of the .install file :

<?php
/**
* Implements hook_install().
*/
function sampletest_install() {
 drupal_install_schema('sampletest');
}
/**
* Implements hook_install().
*/
function sampletest_uninstall() {
  drupal_uninstall_schema('sampletest');
}
/**
* Implements hook_schema().
*/

function sampletest_schema() {
  $schema['sampletest'] = array(
    'description' => t('EMI table'),
    'fields' => array(
      'id' => array(
        'description' => t('My unique identifier'),
        'type' => 'int',
        'unsigned' => true,
        'not null' => true,
      ),
      'amount' => array(
        'description' => t('AMOUNT'),
        'type' => 'bigint',
        'unsigned' => true,
        'not null' => true,
      ),
      'rate' => array(
        'description' => t('RATE'),
        'type' => 'bigint',
        'unsigned' => true,
        'not null' => true,
      ),
      'time' => array(
        'description' => t('TIME'),
        'type' => 'bigint',
        'unsigned' => true,
        'not null' => true,
      ),
    ),
    'primary key' => array('id'),
  );
  return $schema;     
}

2 Answers 2

1

I can see 2 problems with in your code:

  1. You should not call drupal_install_schema() from hook_install() in Drupal 7. hook_schema() will be called and the table will be automatically created/dropped on installing/uninstalling. See here: https://drupal.org/node/224333#install-schema

  2. As @Ollie also stated, there's no field type named bigint. You should use int and set its size to big.

Like following:

'amount' => array(
  'description' => t('Amount'),
  'type' => 'int',
  'size' => 'big',
  'unsigned' => TRUE,
  'not null' => TRUE,
),
0

I think the problem is in using 'bigint' as the type. Use 'type'=>'int', 'size'=>'big', instead.

1
  • And are you sure your 'id' shouldn't be of type serial (auto-increment)?
    – Ollie
    Mar 12, 2014 at 5:42

Your Answer

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

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