Base table or view not found (table names incorrect for long table names)

Created on 3 March 2023, over 2 years ago
Updated 20 March 2023, over 2 years ago

Problem/Motivation

If a field's data table name would be more than 48 characters if it followed the conventional pattern of Entity Type ID + Field Name, the table name will be truncated when it's created (see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...)

Running of the Security Review (if such tables exist) results in a SQL error because it cannot find certain data tables:

The website encountered an unexpected error. Please try again later.Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.localgov_alert_banner__localgov_alert_banner_body' doesn't exist: SELECT `entity_id`, `localgov_alert_banner_body_value` FROM localgov_alert_banner__localgov_alert_banner_body

Proposed resolution

Replace the relevant statements in security_review/src/Checks/Field.php that build the table name

if ($field_storage_definition instanceof FieldStorageConfig) {
              $table = $entity_type_id . '__' . $field_name;
            }
            else {
              $translatable = $entity->isTranslatable();
              if ($translatable) {
                $table = $entity->getDataTable() ?: $entity_type_id . '_field_data';
              }
              else {
                $table = $entity->getBaseTable() ?: $entity_type_id;
              }
              $separator = '__';
              $id = $entity->getKey('id');
            }

... and utilise getDedicatedDataTableName()

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇬🇧United Kingdom robcarr Perthshire, Scotland

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.

  • Issue created by @robcarr
  • @robcarr opened merge request.
  • Status changed to Needs review over 2 years ago
  • 🇬🇧United Kingdom robcarr Perthshire, Scotland

    The MR (and patch) seems to work correctly, but not sure if the $field_storage_definition is not an instance of FieldStorageConfig section requires a rework

  • Status changed to RTBC over 2 years ago
  • 🇬🇧United Kingdom finn lewis Oxford

    Thanks @robcarr

    I can replicate this problem. We're working with LocalGov Drupal distribution.

    https://www.drupal.org/project/localgov

    Steps to reproduce:

    1. Install LocalGov Drupal.
    2. Enable LocalGov Full page Alert Banner (this creates fields on entities with long names)
    3. Install and enable security_review
    4. Run the report at /admin/reports/security-review

    Result:

    Batch fails and we get this error:

    Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.localgov_alert_banner__localgov_alert_banner_body' doesn't exist: SELECT `entity_id`, `localgov_alert_banner_body_value` FROM localgov_alert_banner__localgov_alert_banner_body t; Array ( ) in Drupal\security_review\Checks\Field->run() (line 107 of /var/www/html/web/modules/contrib/security_review/src/Checks/Field.php).

    Apply the patch from this merge request and the report runs just fine!

  • Status changed to Fixed over 2 years ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024