title: Appengine Datastore query returns different result inside transaction
body: <p>Hoping someone can help point out the issue in my code.</p>
  
  <p>I have a query defined outside a transaction, and when it's executed, it correctly matches an existing record in the database.</p>
  
  <p>However, the moment that query is executed inside a transaction, it fails to match the existing records in the database, despite the fact that they exist.</p>
  
  <p>Here's the code, with output below:</p>
  
  <pre><code>// Query for URL to see if any already exist
  existingRemoteURLQuery := datastore.NewQuery("RepoStats").
      Filter("RepoURL =", statsToSave.RepoURL).
      KeysOnly().Limit(1)
  
  testKey, _ := existingRemoteURLQuery.GetAll(ctx, new(models.RepoStats))
  if len(testKey) &gt; 0 {
      log.Infof(ctx, "TEST Update existing record vice new key")
  } else {
      log.Infof(ctx, "TEST No existing key found, use new key")
  }
  
  // Check if we already have a record with this remote URL
  var key *datastore.Key
  
  err := datastore.RunInTransaction(ctx, func(ctx context.Context) error {
      // This function's argument ctx shadows the variable ctx from the surrounding function.
  
      // last parameter is ignored because it's a keys-only query
      existingKeys, err := existingRemoteURLQuery.GetAll(ctx, new(models.RepoStats))
      if len(existingKeys) &gt; 0 {
          log.Infof(ctx, "Update existing record vice new key")
          // use existing key
          key = existingKeys[0]
  
      } else {
          log.Infof(ctx, "No existing key found, use new key")
          key = datastore.NewIncompleteKey(ctx, "RepoStats", nil)
      }
  
      return err
  }, nil)
  </code></pre>
  
  <p>As you can see in the output, the first query outside the transaction correctly matches the existing record.  But inside the transaction, it doesn't recognize the existing record:</p>
  
  <pre><code>2018/08/28 11:50:47 INFO: TEST Update existing record vice new key
  2018/08/28 11:50:47 INFO: No existing key found, use new key
  </code></pre>
  
  <p>Thanks for any help in advance</p>
  
  <p><strong>Updated</strong></p>
  
  <p>Dan's comment lead to printing out the error message on the query inside the transaction:</p>
  
  <pre><code>    if err != nil {
          log.Errorf(ctx, "Issue running in transaction: %v", err)
      }
  </code></pre>
  
  <p>Which prints:</p>
  
  <blockquote>
    <p>ERROR: Issue running in transaction: API error 1 (datastore_v3: BAD_REQUEST): Only ancestor queries are allowed inside transactions.</p>
  </blockquote>