Adding an 'auto_increment' field

Last modified: October 2, 2009 - 14:26

There's now a module to do this, Serial Field, I recommend you to use that one instead of this option

This snippet will create an auto increment field. You could use this if you have a content type where you need to register a transaction number and you don't want to let the users add the transaction number because you might lose the sequence.

if (!empty($node_field[0]['value'])) {  // the node is not new
  return $node_field[0]['value'];
}
else {  // the node is new
  $count = db_result(db_query("SELECT COUNT(*) from {node} WHERE type = 'transaction'"));
  $node_field[0]['value'] = $count;
}

In this case the Content Type is 'Transaction' so you'll have to change that to the content type you want to count...

This is not atomic!

MartysMind - March 11, 2009 - 09:56

Unfortunately this method is NOT ATOMIC. If your server is under heavy load it is quite likely that you will end up with nodes with the same id.

If you require a unique ID you should either:

1. Lock the table when you perform the above operation, or;
2. Add an auto increment or;
3. Base it off of the nid (which is atomic).

-Marty

How?

dejamuse - March 18, 2009 - 13:59

Not being a programmer, can you explain how to do numbers 1 and 2, and which is the preferred method?

I presume by autoincrement you mean in MySQL as shown here:

http://www.plus2net.com/sql_tutorial/mysql_auto_increment.php

And discussed here:

http://forums.whirlpool.net.au/forum-replies-archive.cfm/513273.html

More discussion on this to further confuse me:

http://th.php.net/mysql-insert-id

Seems to be a lot of controversy about this with respect to the true uniqueness of the ID, which I don't quite understand.

Thanks, Jeff

The preferred method really

kentr - June 23, 2009 - 09:57

The preferred method really depends on your specific needs (the specifications for the project).

If you can define a "transaction" (or "item", "record", "node", etc) as a content type and all you need is a unique (or unique and increasing) value for each, just use the node id rather than a computed field. A unique, auto-incremented id is internally created for each node.

As mentioned in one of your links, a drawback is that if you create a "transaction" then delete it, the next one will have another new value, so your list of transaction numbers will skip a value. But I'll go out on a limb and generalize that this is preferred in the majority of record-keeping scenarios (intentionally not recycling ID numbers). If you were testing and running up the numbers, it would be possible with a little extra work inside the database to reset the whole thing once you finish the testing.

Someone else will have to comment on how to effectively use table locking for this case, or whether or not it's even possible.

just use the node id rather

lelizondob - June 23, 2009 - 21:16

just use the node id rather than a computed field

The problem with using the nid is that the nid increase when every node is created. Sometimes you don't want that, you want a unique number who increases for only one content type. Take this example, an invoice, each one has a unique number and you have a sequential number that you have to respect, using the nid it's not an option, if you create the node invoice 1 and then you create some other node and then you create the node invoice 2 you'll have that invoice 2 will have nid = 3. not good.

as for deleting the transaction and skipping the old value I just didn't think of that at the moment I created this snippet but it's a good point.

Luis

Agreed

Parkes Design - August 20, 2009 - 16:53

I'm surprised there isn't a product code / serial number / autonumber CCK module floating around. I've been looking but haven't been able to find one yet.

unique number solution

Alauddin - September 1, 2009 - 20:58

I think a combination of this module plus the unique_field will do the trick.
http://drupal.org/project/unique_field

Serial field

druplicate - October 13, 2009 - 17:14

This new one is supposed to be truly atomic, just like NID.

http://drupal.org/project/serial

Awesome Find!

Parkes Design - October 15, 2009 - 14:08

Great find, I'm excited to try this one out!

Haven't tried using the

druplicate - October 21, 2009 - 18:49

Haven't tried using the serial module yet to insert the serial ID in the node title using the nodeautotitle module. I have a feeling it's the same problem as using NID for this as extensively discussed here:

http://drupal.org/node/194197

this worked nicely for me: if

theamoeba - November 11, 2009 - 13:40

this worked nicely for me:

if (empty($node_field[0]['value'])) {
$count = db_result(db_query("select max(field_client_number_value) from {content_type_customer}"));
$count++;
$node_field[0]['value'] = $count;
}

 
 

Drupal is a registered trademark of Dries Buytaert.