Cannot add new user: duplicate entry for key users.PRIMARY- The "next user ID" counter is fouled up somehow

Starting December 14, 2021, my site began creating users with low user IDs, even though the highest-used user ID was 9317. I thought something had been changed in a core update to reuse IDs that had been freed up by canceling a user account. However, I have just discovered that since that date, the site has been trying to reuse every single ID starting with 1. My error log is filled with errors like this:

DrupalCoreEntityEntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '281' for key 'users.PRIMARY': INSERT INTO "users" ("uuid", "langcode") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 5ff1347c-439f-437e-ba3f-762f7350fda3 [:db_insert_placeholder_1] => en ) in DrupalCoreEntitySqlSqlContentEntityStorage->save() (line 811 of /home/drupal/webroot/rsc.byu.edu/web/core/lib/Drupal/Core/Entity/ Sql/SqlContentEntityStorage.php). 

The above example says Duplicate entry '381'. The next error will say Duplicate entry '382' and so on.

I read posts suggesting the sequences table contains the next user ID value. My sequences table contains the value 9330, which looks about right and would be fine if it were being used. But it’s not.

I hope someone can point me in the right direction with this.

UPDATE 1: After long looking, I noticed on a backup from before December 14, 2021, the users table uid field is defined as Type: int, Attributes: UNSIGNED, Null: No, Default: None. NOW (February 18, 2022), the uid field is defined as Type: int, Attributes: nothing, Null: No, Default: None, AND Extra: AUTO_INCREMENT. The AUTO_INCREMENT wasn’t there before this problem began happening.

Does anyone know whether these changes were part of a Drupal update that changed the way the next user ID is determined?

UPDATE 2: I tried the following command in the database:

ALTER TABLE user AUTO_INCREMENT = 3918; # highest used so far is 3917 

This seems to have solved the problem. I’m hoping someone knows whether a change was made to move away from the sequences table to using AUTO_INCREMENT or if I’m just experiencing something weird.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Cannot add new user: duplicate entry for key users.PRIMARY- The "next user ID" counter is fouled up somehow

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.