Field 'bid' doesn't have a default value

Created on 3 July 2025, 9 days ago

Problem/Motivation

I have updated the Drupal Core from 11.1.8 to 11.2.2 version, when I try to check manually the available updates in /admin/reports/updates I get the error: "Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[HY000]: General error: 1364 Field 'bid' doesn't have a default value: INSERT INTO "batch" ("timestamp", "token", "batch") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 1751552637 [:db_insert_placeholder_1] => [:db_insert_placeholder_2] => ) in Drupal\mysql\Driver\Database\mysql\ExceptionHandler->handleExecutionException() (line 45 of /var/www/html/web/core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php)."

Steps to reproduce

  • Update from Drupal Core 11.1.8 to 11.2.2
  • Run drush updb
  • Go to /admin/reports/updates
  • Click on "Check manually" link.

Proposed resolution

Implements the next hook_update()

/**
 * Implements hook_update_N() 
 *   Update the 'bid' column in the 'batch' table to be AUTO_INCREMENT.
 *
 * @return void
 */
function your_module_update_8009() {

  $connection = \Drupal::database();

  // Check if the 'bid' column is already AUTO_INCREMENT.
  $query = $connection->query("
    SELECT EXTRA
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'batch'
      AND COLUMN_NAME = 'bid'
      AND TABLE_SCHEMA = :schema
  ", [':schema' => $connection->getConnectionOptions()['database']]);

  $result = $query->fetchField();

  if ($result !== 'auto_increment') {
    // Modify the 'bid' column to be AUTO_INCREMENT.
    $connection->query("ALTER TABLE batch MODIFY COLUMN bid INT UNSIGNED NOT NULL AUTO_INCREMENT");

    \Drupal::logger('your_module')->notice("The 'bid' column in the 'batch' table has been modified to be AUTO_INCREMENT.");
  }
  else {
    \Drupal::logger('your_module')->notice("The 'bid' column in the 'batch' table is already AUTO_INCREMENT.");
  }
}
🐛 Bug report
Status

Needs review

Version

11.2 🔥

Component

batch system

Created by

🇨🇴Colombia gustavo.contreras

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

Comments & Activities

  • Issue created by @gustavo.contreras
  • The batch table is auto_increment:

    > describe batch;
    +-----------+------------------+------+-----+---------+----------------+
    | Field     | Type             | Null | Key | Default | Extra          |
    +-----------+------------------+------+-----+---------+----------------+
    | bid       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | token     | varchar(64)      | NO   | MUL | NULL    |                |
    | timestamp | int(11)          | NO   |     | NULL    |                |
    | batch     | longblob         | YES  |     | NULL    |                |
    +-----------+------------------+------+-----+---------+----------------+
    

    Possibly that site had a problem with a restored backup or some other condition.

  • I think this is frequently a backup, move, or restore issue where SQL settings are missed.

  • 🇨🇴Colombia gustavo.contreras

    The site was migrated to a new hosting about 8 months ago.

  • This is essentially the same issue as the one I related so I am marking it a duplicate.

Production build 0.71.5 2024