Thu 12th Apr 2012 at 16:18

Drupal Anonymous Comments Woes

While working on a recently migrated Drupal site I hit a snag that resulted in comments posted by anonymous users seemingly stopped being posted. Thanks to some digging the solution was simple and the cause was obvious! The evils of using auto-increment. 

I can't take any credit in working out what went on, as the solution as presented on the Drupal forum site:

Anonymous user has probably disappeared from users table
Posted by mmatsoo on April 18, 2010 at 4:26pm

I was having this problem and the post about developers accidentally deleting all records from the users table rang a bell. (users table should at least have UIDs 0 and 1 for anonymous and superuser)


Sure enough, I checked my users table and uid = 0 (anonymous user) was not there. I added it manually into the DB and now anonymous posts are showing.

I am not sure why it was deleted in the first place because I hadn't done any direct table deleting/truncating in the DB. However, I have seen this happen in other situations too so it's something to keep in mind when dealing with anonymous user content. Make sure there's a record for the anonymous user (uid=0) in the DB.

The original page can be found here

 

Upon finding this post, I took a look in the users table and sure enough, there was the usual blank row, but with a uid=4 not uid=0. There seems to be some discussion on the page as to why the number changed. This becomes obvious once you realise that the uid field is an auto increment field. So, when you do an export and import, it inserts a record is uid=0 (as it should), but MySQL treats this as a request for the field to be set to the next increment for that field, which in my case was 4.

A simple update statement saves the day:

UPDATE user SET uid=0 WHERE uid=4;

This is a good example of why you should not be lazy and use auto increment fields for such important fields.

This site uses cookies, please read my cookie policy.