Can't update database: Syntax error or access violation: 1075 Incorrect table definition

Created on 17 February 2022, almost 3 years ago
Updated 21 November 2023, about 1 year ago

Problem/Motivation

When trying to update the database I get the following issue report:

[error]  SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key: ALTER TABLE "users" DROP PRIMARY KEY; Array
[error]  Update failed: user_update_9301 
[error]  Update aborted by: user_update_9301 
[error]  Finished performing updates. 

What can I do to fix this?

💬 Support request
Status

Closed: duplicate

Version

9.3

Component
Database 

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

🇳🇱Netherlands zebda

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia taggartj

    This is still an issue in drupal 10 please see

    /**
     * Adds a prime key id so I can finish early today. 
     */
    function MYMODULE_update_8002() {
      // Add a prime key for views base.
      $spec = [
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ];
      $db = \Drupal::database();
      $schema = $db->schema();
    
      if ($schema->fieldExists('some_custom_table', 'id')) {
        $schema->dropField('some_custom_table', 'id');
      }
       
      // THESE HERE Don't work as they should 
      //$schema->addField('some_custom_table', 'id', $spec, ['id']);
      //$schema->addPrimaryKey('some_custom_table', ['id']);
    
      // Resulting to hackery. 
      $table = 'some_custom_table';
      $col = 'id';
      $db->query('ALTER TABLE {' . $table . '} ADD {'. $col .'} INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY ({'. $col .'})');
    }
    
Production build 0.71.5 2024