views:

100

answers:

1

I am using Google AppEngine (Java) and would like to have the primary key to be auto incrementing, preferably increasing by 1.

IdGeneratorStrategy.INCREMENT seems to be not supported in AppEngine.

Is IdGeneratorStrategy.SEQUENCE what I need?

+1  A: 

Auto-incrementing IDs does not work on App Engine because of the way App Engine stores your data spread across multiple servers and potentially data centers.

You can simulate an auto-incrementing ID, but honestly, there is probably no good reason for needing one.

If unique IDs are what you need, those are the default. If you need to know how many rows have been created, that's also easy.

Jason Hall
a broader question: say I write a bugzilla in GAE, and I want the bug IDs to be integer, unique, incremental, mostly continuous. how to do that?
irreputable
If i will to query how many rows there are, for every time a record is created, there will be an extra fetch from the datastore. I hope to have a more elegant solution.
samwize
Do you know how `IdGeneratorStrategy.SEQUENCE` work?
samwize
@irreputable The question then becomes, why do you want your IDs to be constrained like that? Uniqueness is really the only important requirement. Being incremental means having to maintain a state across your app, which severely limits efficiency and scalability.
Jason Hall
@samwize Does SEQUENCE work? I've never tried it. This thread seems to indicate that it doesn't work as you may expect: http://www.mail-archive.com/[email protected]/msg04865.html
Jason Hall
because we humans like that, and we use our computers to make what we like. people like to say `bug#7234`, not `bug#dkfio2qr289uasfj239ujsadfa`. understandably there are restrictions and we don't always get what we want. but rejecting human requirements completely due to a little technical inconvenience, that's not weighing things correctly.
irreputable
@irreputable You can generate a key however you want, for example a random 4-digit number. It's just not scalable to keep the state of the current highest ID across all the servers on which your app will be running, especially when you don't actually need an incrementing number
Jason Hall
What I want to achieve is to fetch new records after a certain ID. In my datastore, I do have the datetime the record is added, but that is not unique. Is there a better way to do this?
samwize
Use the datetime. It doesn't matter if it's unique - it meets your criteria of 'records after x'.
Nick Johnson