Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating unique number sequence for use as entity key for app engine datastore

Has anyone got any example code for creating a unique number sequence to be used as keys for an entity in a Google app engine datastore?

Would like to use sequential order numbers as the key.

like image 529
Ron Chan Avatar asked May 13 '10 10:05

Ron Chan


2 Answers

Use db.allocate_ids() as described here to generate unique IDs for your entities.

Here's a quick example derived from the example at the above link:

from google.appengine.ext import db

# get unique ID number - I just get 1 here, but you could get many ...
new_ids = db.allocate_ids(handmade_key, 1)

# db.allocate_ids() may return longs but db.Key.from_path requires an int (issue 2970)
new_id_num = int(new_id[0])

# assign the new ID to an entity
new_key = db.Key.from_path('MyModel', new_id_num)
new_instance = MyModel(key=new_key)
...
new_instance.put()

(issue 2970 reference)

like image 196
David Underhill Avatar answered Oct 14 '22 10:10

David Underhill


You might want to look at How to implement "autoincrement" on Google AppEngine where you find a implementation of sequence numbers.

like image 2
max Avatar answered Oct 14 '22 08:10

max