Fatal database error when updating to 2.0-beta9: "Invalid use of NULL value"

Created on 7 February 2023, almost 2 years ago
Updated 10 April 2024, 7 months ago

Problem/Motivation

The update hook that was introduced in 🐛 Table entity string IDs should be 255 Fixed is causing a fatal database error:

SQLSTATE[22004]: Null value not allowed: 1138 Invalid use of NULL value: ALTER TABLE "entity_usage" CHANGE `target_id_string` `target_id_string` VARCHAR(255) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL DEFAULT '' COMMENT 'The target ID, when the entity uses string IDs.'

This is happening because the target ID column used to allow NULL values prior to 2.0-alpha5. This was changed in #2980089: Not working with configuration reference , but no update hook was supplied to update the pre-existing rows that contain NULL values.

There has been an earlier attempt to update the column to use NOT NULL and this resulted in exactly the same error, ref. 🐛 Update failed: entity_usage_update_8203 Fixed .

Steps to reproduce

  1. Install Entity Usage 2.0-alpha4.
  2. Recreate the entity usage statistics.
  3. You probably now have thousands of entries where the value NULL exists in the target_id_string column.
  4. Update to 2.0-beta8.

Result: the error above is thrown.

Proposed resolution

Update all entries from the table that have a NULL value for the target_id_string column before declaring it to be NOT NULL:

UPDATE entity_usage SET target_id_string = '' WHERE target_id_string IS NULL;

🐛 Bug report
Status

Fixed

Component

Code

Created by

🇧🇬Bulgaria pfrenssen Sofia

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

Comments & Activities

  • Issue created by @pfrenssen
  • Status changed to Needs review almost 2 years ago
  • Status changed to Fixed almost 2 years ago
  • 🇪🇸Spain marcoscano Barcelona, Spain

    Thanks for filing the ticket and for the fix!
    Committed and tagged beta10 with this fix, since it's better to avoid beta9 at this point.
    https://www.drupal.org/project/entity_usage/releases/8.x-2.0-beta10

  • 🇧🇬Bulgaria pfrenssen Sofia

    Thanks for the quick response!

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed about 1 year ago
  • 🇭🇺Hungary szato

    On entity_usage_update_8206() I got:

    >  [notice] Update started: entity_usage_update_8206                                                                                                                                                   
    >  [error]  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  .: ALTER TABLE `entity_usage` DROP PRIMARY KEY;

    I have to disable 'sql_require_primary_key' on session level by adding code to entity_usage_update_8206():$database->query('SET SESSION sql_require_primary_key=0;');
    before calling:

    // Drop all indices involving columns we want to change.
  • 🇩🇰Denmark Uv516 Denmark

    I got the error as descriped in attached file. It handles about the field "tagert_id_string" which doesn't have a default value.
    After a lot of searching I found the error and the solution:
    In MySQL under entity_usage I inspected the table.
    The field "target_id_string" is created with no NULL-value and no default value.
    The field can not have at NULL-value, but it MUST have at default value.
    I set (in) 0 as the default value and that solved my problem.

    From the patch:

    +  $database->update('entity_usage')
    +    ->fields(['target_id_string' => ''])
    +    ->condition('target_id_string', NULL, 'IS NULL')
    +    ->execute();
    

    I think that line 3 ('->condition('target_id_string', NULL, 'IS NULL')') should be different?

Production build 0.71.5 2024