Google应用引擎:Golang的datastore.GetAll()方法不起作用。

huangapple go评论68阅读模式
英文:

Google app engine: Golang datastore.GetAll() not working

问题

每周一次,一个cron作业在我的一个应用引擎项目中调用以下函数:

func cleanMealsFromDatabase(context appengine.Context) error {
    query := datastore.NewQuery("Essen").Limit(1000).KeysOnly()
    keys, err := query.GetAll(context, nil)
    if err != nil {
        return err
    }
    return datastore.DeleteMulti(context, keys)
}

我正在尝试批量删除一批数据存储实体。该函数被正确调用,但是query.GetAll(context, nil)似乎总是返回一个错误。我做错了什么吗?

英文:

Once a week a cron job calls the following function in one of my app engine projects:

func cleanMealsFromDatabase(context appengine.Context) error {
	query := datastore.NewQuery("Essen").Limit(1000).KeysOnly()
	keys, err := query.GetAll(context, nil)
	if err != nil {
		return err
	}
	return datastore.DeleteMulti(context, keys)
}

I'm trying to batch delete a bunch of datastore entities. The function gets called correctly, but

query.GetAll(context, nil)

seems to always return an error. Am I doing something wrong?

答案1

得分: 0

好的,我现在知道出了什么问题。当我最终弄清楚如何在应用引擎中记录错误时,它显示的错误信息是:

API错误1(datastore_v3: BAD_REQUEST):无法在单个调用中写入超过500个实体

所以我所需要做的就是将Limit参数从1000更改为500,像这样:

query := datastore.NewQuery("Essen").Limit(500).KeysOnly()

现在它按预期工作了。

英文:

Okay, I now know what went wrong. When I finally figured out how to log an error in app engine, it said:

API error 1 (datastore_v3: BAD_REQUEST): cannot write more than 500 entities in a single call

So all I had to do, was to change the Limit parameter from 1000 to 500 like this:

query := datastore.NewQuery("Essen").Limit(500).KeysOnly()

Now it works like intended.

huangapple
  • 本文由 发表于 2016年1月12日 01:56:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/34728033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定