在Google App Engine – Go API中遇到了与查询/数据存储相关的问题。

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

Trouble with Queries/Datastore in Google App Engine - Go API

问题

我正在使用Go APIs玩Google App Engine,尽管我尝试了一切,但无法使Queries返回数据存储中的数据。我可以看到Put()按预期工作,并且可以从dev_appserver.py提供的管理控制台中列出/访问实体。

我存储的结构定义如下:

type TweetData struct {
    Id int64 `datastore:",noindex" json:"id"`
    Text string `datastore:",noindex" json:"text"`
    Screen_name string `json:"screen_name"`
}

我的查询调用如下:

func getDatastoreTweets(c appengine.Context, twitterUser string) []*TweetData {
    q := datastore.NewQuery("TweetData").Filter("Screen_name =", twitterUser).Order("-Id").Limit(10)

    var oldTweets []*TweetData
    if _, err := q.GetAll(c, &oldTweets); err != nil {
        fmt.Printf("Getall had non-nil error! %v\n", err)
    }   

    return oldTweets
}

对于这个查询,err始终为非nil,但总是返回0个结果,即使控制台告诉我有很多结果。我也尝试过在没有Filter调用的情况下进行查询,但没有成功,就像SDK提供的guestbook示例一样。

如果这是一个键的问题(如果我理解正确的话,这是不太可能的,因为我是在一个属性上进行查询),则Put的调用如下:

// tweetData通过参数传递...
key := datastore.NewIncompleteKey(c, "TweetData", nil)
_, err := datastore.Put(c, key, &tweetData)

任何帮助将不胜感激,谢谢!^_^

英文:

I'm playing with Google App Engine using the Go APIs and despite everything I've tried I can't get the Queries to return data that is in the datastore. I can see that Put() works as expected, and the entities are listable/accessible from inspection of the Admin Console available from dev_appserver.py

The struct I'm storing is defined this way:

type TweetData struct {
    Id int64 `datastore:",noindex" json:"id"`
    Text string `datastore:",noindex" json:"text"`
    Screen_name string `json:"screen_name"`
}

And my calls to query it are such:

func getDatastoreTweets(c appengine.Context, twitterUser string) []*TweetData {
    q := datastore.NewQuery("TweetData").Filter("Screen_name =", twitterUser).Order("-Id").Limit(10)

    var oldTweets []*TweetData
    if _, err := q.GetAll(c, &oldTweets); err != nil {
        fmt.Printf("Getall had non-nil error! %v\n", err)
    }   

    return oldTweets
}

For this query, err is never non-nil, but always returns 0 results, even when the Console tells me there are many. I've tried it without the Filter call as well, after the guestbook example the SDK provides, to no avail.

In case it's an issue with keys (unlikely if I understand correctly, because I'm querying on a property), the call to Put is as follows:

// tweetData passed in via parameter...
key := datastore.NewIncompleteKey(c, "TweetData", nil)
_, err := datastore.Put(c, key, &tweetData)

Any help would be appreciated, thanks! ^_^

答案1

得分: 2

查询要求按Id desc排序,而索引字段未索引,您应该选择以下操作之一:

  • 重写TweetData注释以索引Id字段:
    Id int64 `json:"id" datastore:"-"` // 添加datastore标签以索引字段
  • 删除查询的Order子句:
    q := datastore.NewQuery("TweetData").Filter("Screen_name =", twitterUser).Limit(10)
英文:

The query ask for ordering by Id desc, while the index field is un-indexed, you should either:

  • rewrite the TweetData annotation to index Id field:
    Id int64 `json:"id"`
  • Remove the Order clause of your query:
    q := datastore.NewQuery("TweetData").Filter("Screen_name =", twitterUser).Limit(10)

huangapple
  • 本文由 发表于 2012年9月14日 04:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/12414148.html
匿名

发表评论

匿名网友

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

确定