0

I'm on Drupal 10.1.6, and the above-mentioned error is appearing on my status page under "Mismatched entity and/or field definitions." I had similar errors for fields on different entities (File, Content, and Paragraph) that I was able to resolve with one of the following two methods:

  1. Running drush entity-updates.
  2. Navigating to the settings page for the specific fields that needed updating and re-saving.

Method 1 does not work for the User entity, and method 2 is not available, as there is no field settings page available for the User ID field in the Drupal UI. Is there a method for re-saving the User ID field, either programmatically for via drush? Or has anyone else found a different way to resolve this error?

2
  • I am not sure if this is the same person asking at drupal.org/project/drupal/issues/3405944, but a core maintainer commented there.
    – cilefen
    Dec 5, 2023 at 20:59
  • Nope @cilefen, not the same person, but that's a big help. Thanks for pointing me in that direction.
    – JOakland
    Dec 6, 2023 at 0:28

1 Answer 1

1

I posted a solution to this on this thread over on Drupal.org, repeating it here just in case anyone has the same issue. This problem is the result of core update 9301 from D9 being skipped (for some reason), so I grabbed the original update, created a custom module, and tweaked the update function so that I could run it from the module. Here is the rewritten function:

function _user_update_9301() {
   $connection = \Drupal::database();
   
   if ($connection->databaseType() === 'mysql') {
      $sql_mode = $connection->query("SELECT @@sql_mode;")->fetchField();
      $connection->query("SET sql_mode = '$sql_mode,NO_AUTO_VALUE_ON_ZERO'");
      $new_keys = [];
   }
   else {
      $new_keys = ['primary key' => ['uid']];
      $connection->schema()->dropPrimaryKey('users');
   }
   $connection->schema()->changeField('users', 'uid', 'uid', ['type' => 'serial', 'not null' => TRUE], $new_keys);
   if (isset($sql_mode)) {
      $connection->query("SET sql_mode = '$sql_mode'");
   }

   // Update the last installed schema to reflect the change of field type.
   $installed_storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
   $field_schema_data = $installed_storage_schema->get('user.field_schema_data.uid');
   $field_schema_data['users']['fields']['uid']['type'] = 'serial';
   $installed_storage_schema->set('user.field_schema_data.uid', $field_schema_data);

   // The new PostgreSQL sequence for the uid field needs to start with the last
   // used user ID + 1 and the sequence must be owned by uid field.
   // @todo https://drupal.org/i/3028706 implement a generic fix.
   if ($connection->driver() == 'pgsql') {
      $maximum_uid = $connection->query('SELECT MAX([uid]) FROM {users}')->fetchField();
      $seq = $connection->makeSequenceName('users', 'uid');
      $connection->query("ALTER SEQUENCE " . $seq . " RESTART WITH " . ($maximum_uid + 1) . " OWNED BY {users}.uid");
   }
}

Add this function to the .module file of your custom module, and then you can run it via Drush:

drush php-eval '_user_update_9301();'

This will update the user ID field, and the error message will no longer appear on the Drupal status page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.