Get entity by int Id in GAE in GO

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

Get entity by int Id in GAE in GO

问题

我正在尝试通过其整数ID获取实体的Key(而不是实体本身,而是它的Key)(从长远来看,我这样做是为了找到实体的父级)。

从DatastoreViewer中获取的数据:

实体种类:File
实体Key:ag9kZXZ-dHJhc2hib3hhcHByIgsSBEZpbGUYgICAgICAwAoMCxIERmlsZRiAgICAgIDACww
ID:6473924464345088
父级:ag9kZXZ-dHJhc2hib3hhcHByEQsSBEZpbGUYgICAgICAwAoM
文件:id=5910974510923776

我这样做:

k := datastore.NewKey(c, "File", "", 6473924464345088, nil)
currentDirQuery := datastore.NewQuery("File").Filter("key =", k).KeysOnly()
keys, err := currentDirQuery.GetAll(c, nil)

keys的长度为0。我做错了什么?

英文:

I'm trying to get entity Key by its int Id. (not the entity itself, but it's Key) (in the long run I do it to find entities parent)

Data from DatastoreViewer:

Entity Kind
File
Entity Key
ag9kZXZ-dHJhc2hib3hhcHByIgsSBEZpbGUYgICAgICAwAoMCxIERmlsZRiAgICAgIDACww
ID
6473924464345088
Parent
ag9kZXZ-dHJhc2hib3hhcHByEQsSBEZpbGUYgICAgICAwAoM
File: id=5910974510923776

I do it like this:

k := datastore.NewKey(c, "File", "", 6473924464345088, nil)
currentDirQuery := datastore.NewQuery("File").Filter("__key__ =", k).KeysOnly()
keys, err := currentDirQuery.GetAll(c, nil)

The length if keys is 0. What do I do wrong?

答案1

得分: 1

如果你已经有了键,为什么还要进行只返回键的查询来匹配键呢?为什么不直接使用datastore.Get()和键进行查询呢?

至于为什么你的只返回键的查询不起作用,是因为你在构造键时没有包含祖先,你的示例中的键显示了一个父键ag9kZXZ-dHJhc2hib3hhcHByEQsSBEZpbGUYgICAgICAwAoM,这个键的urlsafe版本必须有一个父键,如果你指定了一个祖先。

使用Python我们可以解码这个键:

> ndb.Key(urlsafe="ag9kZXZ-dHJhc2hib3hhcHByIgsSBEZpbGUYgICAgICAwAoMCxIERmlsZRiAgICAgIDACww")
Key('File', 5910974510923776, 'File', 6473924464345088, app='dev~trashboxapp')

可以看到父键是Key('File', 5910974510923776)

你无法使用你在查询中创建的键对子项进行部分匹配。你只能执行祖先查询,这将返回祖先及其所有子项,无论层次结构的深度如何。

这也意味着使用你在示例代码中创建的键进行datastore.Get()将失败。

因此,构造你的键时要包含祖先-请参阅文档https://developers.google.com/appengine/docs/go/datastore/entities#Go_Retrieving_an_entity

但是说实话,除非这只是一个理解正在发生的事情并尝试往返一个键->查询->键的练习,否则你所做的完全是多余的。

英文:

If you have the key already why are doing a keys only query matching the key ? Why don't you just do a datastore.Get() with the key?

As to why your keys_only query is not working, you are not including the ancestor in the key you are constructing , the key in your example has a parent you showed ag9kZXZ-dHJhc2hib3hhcHByEQsSBEZpbGUYgICAgICAwAoM this urlsafe version of the key must have a parent if you are specifying an ancestor.

Using python we can decode this key

> ndb.Key(urlsafe="ag9kZXZ-dHJhc2hib3hhcHByIgsSBEZpbGUYgICAgICAwAoMCxIERmlsZRiAgICAgIDACww")
Key('File', 5910974510923776, 'File', 6473924464345088, app='dev~trashboxapp')

See the parent Key is Key('File', 5910974510923776)

You can not perform a partial match on a child with the key you created for the query. You can only perform ancestor queries which will return the ancestor and all of it's children irrespective of the depth of the heirarchy.

This also means a datastore.Get() will fail with the key you have created in your example code.

So construct you key so that it includes the ancestor - see the docs https://developers.google.com/appengine/docs/go/datastore/entities#Go_Retrieving_an_entity

But to be honest what you are doing is completely redundant unless it's just an excercise in understanding whats going on and your trying to roundtrip a key -> query -> key

huangapple
  • 本文由 发表于 2014年4月17日 14:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/23125866.html
匿名

发表评论

匿名网友

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

确定