0

The Code:

In my module.install i am declaring my main database table. This works without any problems:

function my_module_install() {
  create_initial_roles();
  drupal_set_message("Installed module my_module.");
}

function my_module_schema() {
  $schema['my_module_foo'] = [
    'description' => 'Main table for my_module.',
    'fields' => [
      'id' => [
        'description' => 'The ID of the database entry of foo.',
        'type' => 'serial',
      ],
      // Many more fields ...
    ],
     'primary key' => ['id'],
  ];
  // Second table goes here (see code below).
  return $schema;
}

But as soon as i'm adding a second table, i am getting a SQL error:

// Inserted in shema function (see comment above).
$schema['my_module_bar'] = [
    'description' => 'Secondary table for my_module.',
    'fields' => [
      'id' => [
        'description' => 'The ID of the database entry of bar.',
        'type' => 'serial',
      ],
      // Possibly more fields.
      'primary key' => ['id'],
    ],
  ];

The Error:

The error occurs when installing the module:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Main table fo' at line 3: CREATE TABLE {my_module_bar} ( id INT auto_increment DEFAULT NULL COMMENT 'The ID of the database entry of the bar.', primary key DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Main table for my_module.'; Array ( ) in db_create_table() (line 2776 of C:\xampp\htdocs\drupal\includes\database\database.inc).

It looks like it is a problem with the character set (utf8) but it is a bit weird, that this error does not occur with only my main database table.

1

You're including the primary key as a field, which won't work. It needs to be on the same level as fields:

'fields' => [
  'id' => [
    'description' => 'The ID of the database entry of bar.',
    'type' => 'serial',
  ],
],
'primary key' => ['id'],
  • Argh, stupid mistake. Thanks! :) – Xyarvius Apr 4 at 9:14

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.