You are correct that the best way to get around key restrictions on starting with digits is to add some string prefix. However, the problem with this code is that you also need to take care when modifying the uid for a user. There is no way to change a datastore key. Instead, you would have to create a new entity (with the new uid) and delete the old one (with the old uid). And, since you're not using entity groups here it won't be possible to do that transactionally. To get around this, you could make the new entity a child of the old entity and that would work (it's perfectly to delete a parent entity).
On Oct 25, 7:30 pm, Alexis Bellido <[EMAIL PROTECTED]> wrote: > Hello everybody, I needed to store some information about my users (I > don't need to use Google accounts) and created a model called FbUser, > each entity in the model is a fbuser and each fbuser has a unique user > uid, a field I call 'uid'. > > A uid can't appear more than once in the datastore but it could > eventually change for a user, so it's not inmutable. It can't be used > directly as a key_name because it always starts with a number, in fact > is always a ten digit number. > > I want to make sure that I only store unique uid's in the datastore so > I thought about Model.get_or_insert: > > http://code.google.com/appengine/docs/datastore/modelclass.html#Model... > > and found this suggestion by Dado (thanks a lot for it): > > http://groups.google.com/group/google-appengine/browse_thread/thread/... > > I implemented it for my case like this: > > class FbUser(db.Model): > """ > You can then call FbUser.get_or_insert_by_uid('foo') and get back an > FbUser instance with that unique identifier (it gets created if it > does not yet exists).Model.get_or_insert is automatically wrapped in > a transaction > """ > > uid = db.StringProperty(required=True) > > @staticmethod > def get_or_insert_by_uid(uid): > # prefix with unique identifier to qualify and avoid beginning > with digits, which datastore does not accept > key_name = 'uid:'+uid > return FbUser.get_or_insert(key_name, uid=uid) > > And then I can 'get or create' a fbuser with uid '123' using this: > > FbUser.get_or_insert_by_uid('123') > > I've tested and it works but I want to be sure if this is the right > way of doing it. What do you think? > > Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~----------~----~----~----~------~----~------~--~---
