【发布时间】:2018-08-28 11:59:30
【问题描述】:
希望有人能帮助指出我代码中的问题。
我在事务外部定义了一个查询,当它执行时,它正确匹配数据库中的现有记录。
但是,在事务中执行查询时,它无法匹配数据库中的现有记录,尽管它们存在。
这是代码,输出如下:
// 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) > 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) > 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)
正如您在输出中看到的,事务外的第一个查询与现有记录正确匹配。但在事务内部,它不识别现有记录:
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
提前感谢您的帮助
更新
Dan 的评论导致在事务内部的查询中打印出错误消息:
if err != nil {
log.Errorf(ctx, "Issue running in transaction: %v", err)
}
哪些打印:
错误:事务中运行的问题:API 错误 1(datastore_v3:BAD_REQUEST):事务中只允许祖先查询。
【问题讨论】:
-
FWIW,在 python 中,甚至不允许在事务内部进行非祖先查询(引发异常),也许您看到的是这种情况下特定于 go 的行为。见What can be done in a transaction
-
@DanCornilescu 原来是答案。我没有检查返回的错误值。添加了一条日志语句以将其打印出来,并给出以下响应:
existingKeys, err := existingRemoteURLQuery.GetAll(ctx, new(models.RepoStats)) if err != nil { log.Errorf(ctx, "Issue running in transaction: %v", err) }
标签: google-app-engine go google-cloud-datastore