英文:
Filtering datastore results by parent entity
问题
这个问题已经在Python中得到了回答:
https://stackoverflow.com/questions/7868333/how-to-get-all-records-from-gae-datastore-with-particular-parent
在Go中我该怎么做呢?我想要做类似这样的操作:
t := new(TagRecord)
k, err := datastore.DecodeKey(r.URL.Path[1:])
...
_, err = datastore.NewQuery("TagRecord").
Filter("Parent =", k).
Order("-CreatedAt").
Limit(1).
Run(c).Next(t)
...但是这个操作失败了,并显示以下错误:
datastore: query has no more results
当我尝试使用其他属性进行过滤时,包括那些硬编码到过滤器中和通过URL传递的属性,查询可以正常运行并将正确的属性填充到t中。请问有什么简单的方法可以解决我的问题吗?
英文:
This question as already been answered for Python:
https://stackoverflow.com/questions/7868333/how-to-get-all-records-from-gae-datastore-with-particular-parent
How do I do it in Go? I would like to do something like:
t := new(TagRecord)
k, err := datastore.DecodeKey(r.URL.Path[1:])
...
_, err = datastore.NewQuery("TagRecord").
Filter("Parent =", k).
Order("-CreatedAt").
Limit(1).
Run(c).Next(t)
...but this fails miserably with the following error:
datastore: query has no more results
When I try filtering by other properties, including those hard coded into the filter and those passed through the URL, the query runs properly and populates t with the proper properties. With what humiliating simplicity can my problem be fixed?
答案1
得分: 2
这里让你困惑的是,通过父级查询不使用Filter()。相反,你需要使用祖先约束:
q := datastore.NewQuery("TagRecord").
Ancestor(k).
Order("-CreatedAt").
Limit(1)
// 等等...
英文:
What's tripping you up here is that querying by parent does not use Filter(). Instead, you use an ancestor constraint:
q := datastore.NewQuery("TagRecord").
Ancestor(k).
Order("-CreatedAt").
Limit(1)
// etc...
答案2
得分: 0
确保您还为此特定查询定义了索引,并上传索引配置文件。
英文:
Make sure you also define the index for this specific query and upload the index config file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论