Database server table creations fail if sql_require_primary_key is set

Created on 2 February 2021, over 4 years ago
Updated 13 July 2024, about 1 year ago

Problem/Motivation

I have a managed MySQL database from DigitalOcean. Their configuration has sql_require_primary_key set. The entities cannot be indexed as table creation fails since search_api_db_* does not specify a primary key.

Drupal\search_api\SearchApiException: SQLSTATE[HY000]: General error: 3750 Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message. Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting.: CREATE TABLE {search_api_db_roasts_roast_level} ( `item_id` VARCHAR(150) NOT NULL COMMENT 'The primary identifier of the item' ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8mb4; Array ( ) in Drupal\search_api_db\Plugin\search_api\backend\Database->fieldsUpdated() (line 1122 of /workspace/web/modules/contrib/search_api/modules/search_api_db/src/Plugin/search_api/backend/Database.php).

Steps to reproduce

Create a database server on MySQL 8 with sql_require_primary_key set. Index values. Indexing fails.

Proposed resolution

Set item_id as a primary key?

Determine if the changes belong in DatabaseCompatibility\MySql

Or here directly https://git.drupalcode.org/project/search_api/-/blob/8.x-1.x/modules/sea...

      // For the denormalized index table, add a primary key right away. For
      // newly created field tables we first need to add the "value" column.
      if ($type === 'index') {
        $table['primary key'] = ['item_id'];
      }

I see that the primary key is set on the table at the end of the process

   // Add a covering index for field tables.
    if ($new_table && $type == 'field') {
      $this->database->schema()->addPrimaryKey($db['table'], ['item_id', $column]);
    }

Maybe we can set the initial primary key as item_id by default, and then at the end of the function drop it and add a new one of item_id and the column name.

Remaining tasks

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Database backend

Created by

πŸ‡ΊπŸ‡ΈUnited States mglaman WI, USA

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

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024