Hello,today,as i was porting a module to drupal 7, i noticed that uid for the users table has not autoincrement?

Is this the expected behavior (and why),or something went bad during my install?

And if this is the expected behavior at least mention the change here: http://drupal.org/update/modules/6/7
since uid was auto-increment in drupal 6.Cheers

Comments

ParisLiakos’s picture

Version: 7.4 » 7.x-dev
regginos’s picture

Priority: Normal » Major

I just noticed that too. Trying to insert into that table using db_insert() gives an error. Why does the "users" table have no auto_increment field?

droplet’s picture

I guess one of the reason is innodb auto-increment isn't reliable.

eg. while hit error, innodb keeps increase ONE in auto-increment but MyISAM don't.

regginos’s picture

So how is the uid in the "users" table desided? I tried to create a couple of test users and they are given quite strange uid's and there are gaps in the uid's (users have uid's: 0,1,14,19,20. Why not 3,4, etc.?).

marcingy’s picture

Status: Active » Closed (works as designed)

The next value is pulled from the sequences table this is by design. Calling drupal_write_record will increment to next available sequence if no uid is provided.

regginos’s picture

So I should call drupal_write_record instead of db_insert (I want to create some specific users during install of a module I am writing). Am I correct?
To honest, I tried

db_drop_primary_key('users');
db_change_field('users', 'uid', 'uid',
array('type' => 'serial', 'not null' => TRUE, 'description' => 'Primary key: Unique user\'s ID.'),
array('primary key' => array('uid')));

before adding the new users, but doesn't work either.

marcingy’s picture

You can simply call user_save I believe and pass in the appropriate data via that api. All enitity operations should user the drupal api.

eg

  $edit = array();
  $edit['name']   = 'name';
  $edit['mail']   = $edit['name'] . '@example.com';
  $edit['pass']   = user_password();
  $edit['status'] = 1;

  $account = user_save(drupal_anonymous_user(), $edit);

This would create a user with called name, with a given email adress, a random password and make them active.

marcingy’s picture

You can even amend account to be

$account->uid = 4;
$account->is_new = TRUE;

However this will fail if uid 4 already exists and to be honest I would never build a module based on needs specific uids beyond 0 and 1, the uids available are going to be inconsistent. The only time this makes sense is doing a data import and there is a need to preserve data. If you need specific users you should define roles and assign the role to a user and then test if the give user has access to do somthing via user_access function.

regginos’s picture

Thank you, marcingy! Your comments have been very helpfull to me. Yes, I agree that the only time this makes sense is data import to those users accounts. I need them as dummy accounts to store a value in a new field the module creates in the users table. Those dummy user-accounts just represent a some robots that will function in my site and will never be used for log-in. They are created with status "0". I think it makes sense.

There is something else bothering me: as a new programmer and complete novice in Drupal how was I supposed to find out about the existence of the user_save() function? Searching drupal for "add new user" or "adding new users in database" doesn't give very helpfull results. Of caurse when you know the function's name, you can easily find it's documentation. Is there a nice way to see what functions are available in drupal API?

marcingy’s picture

No problem glad the above info was useful. Drupal has an online reference http://api.drupal.org/api/drupal and specifically you are likely to be interested in http://api.drupal.org/api/drupal/functions/7 and http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7. Also the examples module is really helpful reference point http://drupal.org/project/examples.

ParisLiakos’s picture

Priority: Major » Normal

still...i dont get why node is AUTO_INCREMENT and user is not.....seems inconsistent to me...anyways

marcingy’s picture

You can find more info here http://drupal.org/node/356074

W.M.’s picture

Issue summary: View changes

User table must have its own auto-increment. This issue must be taken seriously. On one site of mine, I had the Uid jump from 13 to 1130 because of the shared auto-increment value with other Drupal core modules.

drupalfan2’s picture

How can I write a patch for this problem for Drupal 8/9?

uid should be consistent without gaps. Any idea for a patch?