我正在尝试使用 golang 将一个项目放入 Google 数据存储。
不过,我总是遇到datastore: invalid key
错误,无法弄清楚这里出了什么问题。我正在使用"cloud.google.com/go/datastore"
包。
首先,我尝试获取父节点的密钥(不确定这是正确的方法,但我最终会得到一个datastore.Key
parentKey
(。
现在使用parentKey
作为父项创建新密钥,然后尝试使用此newKey
put
项目时,我收到invalid key
错误消息。
q := datastore.NewQuery("Supplier")
.Namespace("inventory")
.Filter("Name =", "supplier-01")
.Limit(1)
var s []supplier
parentKey, err := client.GetAll(ctx, q, &s)
if err != nil || len(parentKey) < 1 {
fmt.Printf("could not find parent key: %vn", err)
return
}
newKey := datastore.IncompleteKey("InventoryItem", parentKey[0])
//newKey := datastore.NameKey("InventoryItem", item.Name, parentKey[0])
if _, err := client.Put(ctx, newKey, &item); err != nil {
fmt.Printf("could not save item: %vn", err)
return
}
我用NameKey
和IncompleteKey
都试过了,但都没有运气。
我显然在这里缺少一些东西,但无法弄清楚它是什么以及如何将我的项目作为另一个节点的子节点写入数据存储。
汤米,你在评论中钉住了它。 您需要在新密钥上设置命名空间。 我在Cloud Datastore Go文档中看不到隐式执行此操作的方法,因此在调用Put((之前,您必须执行newKey.namespace = parentKey.namespace。