Kindless Queries in App Engine Go

微笑、不失礼 提交于 2019-12-08 08:28:27

问题


In Python it's

q = db.Query()
q.ancestor(ancestor_key)

I tried:

q := datastore.NewQuery("")
q.Ancestor(ancestor_key)

I get the error "datastore: empty kind" when running GetAll

I also tried:

q := &datastore.Query{}
q.Ancestor(ancestor_key)

I get the error "datastore: empty query kind"

Thanks in advance for any help with this matter.


回答1:


func NewQuery

func NewQuery(kind string) *Query

NewQuery creates a new Query for a specific entity kind. The kind must be non-empty.

In your code,

q := datastore.NewQuery("")

the kind is empty.




回答2:


Rich Churcher's comment seems to be right, at least at this point in time.

I don't think the Python kindless ancestor query is supported in Go. For a moment there I thought you could use the ancestor key's Kind() method, then I had some more coffee and came to my senses.




回答3:


GetAll doesn't seem to work, but you can do kindless queries.

ctx := appengine.NewContext(r)
q := datastore.NewQuery("")
for it := q.Run(ctx); ; {
  key, err := t.Next(nil)
  if err == datastore.Done {
    break
  }
  if err != nil {
    break
  }
  fmt.Printf("%v\n", key)
}


来源:https://stackoverflow.com/questions/14948440/kindless-queries-in-app-engine-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!