title: google app engine datastore unique id
body: <p>On Google App Engine, I would like to create a unique key for a data table. As suggested by <a href="https://developers.google.com/appengine/docs/python/datastore/overview#Kinds_Keys_and_Identifiers" rel="nofollow">this</a>, I am trying to do the following:</p>
  
  <p>Suppose I have a model called Message, and the field values are obtained from a web page as follows:</p>
  
  <pre><code>message = db.Message()
  message.name = self.request.get('name')
  message.email = self.request.get('email')
  ...
  message.put()
  message.messageId = message.key().id()
  message.put()
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li><p>Is this the correct way of creating a unique id (messageId)? I mean: put the entity first, get it's id and assign to messageId, and finally put again? It looks strange.</p></li>
  <li><p>How to prevent the concurrency problem? I know that I can use 'transaction' as follows:</p>
  
  <pre><code>@db.transactional
  def storeEntity():
      message.name = self.request.get('name')
      message.email = self.request.get('email')
      ...
      message.put()
      message.messageId = message.key().id()
      message.put()
  </code></pre></li>
  </ol>
  
  <p>But what if this transaction fails? How do I do it until it is successful? Thanks in advance.</p>