I have noticed at my Drupal 7 that there are gaps between the uid's of new users. The UID of a new user should increase by one from the highest assigned UID in database. However, I have noticed gaps of even a thousand.

Inside user.module file, there is the following code:

// Allow 'uid' to be set by the caller. There is no danger of writing an
      // existing user as drupal_write_record will do an INSERT.
      if (empty($account->uid)) {
        $account->uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());

I have made the QB query read like this:

$account->uid = db_next_id(db_query('SELECT MAX(uid) FROM users')->fetchField());

It appears that this solves the issue. My question, is there really a mistake inside the core user.module file? Is it safe to modify the file the way I did? Thanks.


The above does not make a difference after all. I have changed the line to this:

$account->uid = db_query('SELECT MAX(uid) FROM users')->fetchField() + 1;

This works with no reference to db_next_id. For me it is very important to increase the uid's always by one and this is one way to do it.

1 Answer 1


uid and other identifiers are not sequential since Drupal 7. Identifiers are generated using a sequence API, see this issue: https://www.drupal.org/node/356074

If you check the db_next_id you'll see that the number passed as argument is just de minimum id that is considered valid.

See also this question and answer: What could cause uids to not be numbered consecutively?


Your Answer

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

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