Kindless Queries in App Engine Go

2019-06-06 08:01发布

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.

3条回答
太酷不给撩
2楼-- · 2019-06-06 08:28

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楼-- · 2019-06-06 08:30

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-06-06 08:32

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)
}
查看更多
登录 后发表回答