I am trying to add a transaction so as not to create two objects with the same attribute. In my application, I create a new Player every time I see a new Google user. My current implementation sometimes creates duplicate players when several json calls are made by a new Google user within a few milliseconds. When I add a transaction, such as commented out here, I get various errors. What is the easiest way to ensure that I never create two players with the same user_id?
def get_player_from_user(self, user_id):
player = Player.all().filter('user_id =', user_id).get()
if not player:
player = self.create_new_player(user_id)
return player
def create_new_player(self,user_id):
player = Player.all().filter('user_id =', user_id).get()
if player:
return player
player = Player()
player.user_id = user.user_id()
player.put()
return player
source
share