I am building an App Engine app that uses OAuth to get a user’s data from an external API. The user is assigned a numerical id by the external API. I was considering giving my User model an id field which would store a copy of the external user id. Then I remembered that DataStore records can be given keys with a manually assigned numerical id.
external_id = int(api_response['user']['id'])
handmade_key = db.Key.from_path('User', external_id)
By manually constructing the key before creating the record I would then be able to retrieve the record using this id alone.
user = User.get_by_id(external_id)
However, I understand that I would need to allocate the entire range of ids that the API might use.
allocate_ids(User, 10000)
My first question is; when should I call allocate_ids? Seems like I should only have to execute it once, but if it executes each time an instance of my app is spun up will that effect performance? If I never put User entities without a manually constructed Key do I still need to allocate_ids?
The second question is; am I mad!? Doing it the regular way isn’t much more troublesome
user = User.all().filter('external_id =', external_id)
Is it worth the hassle of allocating ids to avoid that DataStore query?
You don’t need to
allocate_idsif you always construct keys in the manner you describe. It’s only necessary when you expect to also allow the Datastore to create its own auto-incrementing numeric IDs.And yes, you are almost always better off with fewer fields and indices, so create the Key using the external unique ID value, as you describe.