Change appengine ndb key


Change appengine ndb key



I have a game where I've (foolishly) made the db key equal to the users login email. I did this several years ago so I've got quite a few users now. Some users have asked to change their email login for my game. Is there a simple way to change the key? As far as I can tell I'd need to make a new entry with the new email and copy all the data across, then delete the old db entry. This is the user model but then I've got other models, like one for each game they are involved in, that store the user key so I'd have to loop though all of them as well and swap out for the new key.



Before I embark on this I wanted to see if anyone else had a better plan. There could be several models storing that old user key so I'm also worried about the process timing out.



It does keep it efficient to pull a db entry as I know the key from their email without doing a search, but it's pretty inflexible in hindsight




1 Answer
1



This is actually why you don't use a user's email as their key. Use ndb's default randomly generated key ids.



The efficiency you're referring is not having to query the user's email to retrieve the user id. But that only happens once on user login or from your admin screens when looking at someones account.



You should rip the bandade off now and do a schema-migration away from this model.


UsersV2


user_v2 = ndb.KeyProperty(UsersV2)



You should use the taskqueue to do something like this and then you won't have to worry about the process timing out:



https://cloud.google.com/appengine/articles/update_schema



Alternatively, if you are determined to do this cascading update everytime a user changes an email, you could set up a similar update_schema task for just that user.


update_schema





Thanks for the advice. I thought about adding a property to my existing model for the email and crawl all users to set that.. then for any new users go with random ids.. then change all my user fetching code to search for the prop instead of the key. So I just ignore the fact that the id text is meaningful. Then all my models that reference them wouldn't have to change. Does this seem like reasonable migration path?
– Daniel
Jul 2 at 6:11





Yea that should work. I could've swore I read that you shouldn't mix app engine generated ids with custom ids, but I'm unable to find anything saying that now. If you do need to generate your own random ids, you would use something like this docs.python.org/3/library/uuid.html#uuid.uuid1. The only other thing to keep in mind is constructing a User key by key id ndb.Key(User, 71321839) vs by name ndb.Key(User, 'user@example.com')). By id, the second parameter has to be a long().
– Alex
Jul 2 at 17:07


ndb.Key(User, 71321839)


ndb.Key(User, 'user@example.com'))


long()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?