gpt4 book ai didi

Google Datastore 无效 key 错误

转载 作者:数据小太阳 更新时间:2023-10-29 03:10:56 25 4
gpt4 key购买 nike

我正在尝试使用 golang 将项目放入 Google 数据存储区。

虽然我总是遇到 datastore: invalid key 错误,但无法弄清楚这里出了什么问题。我正在使用 "cloud.google.com/go/datastore" 包。

首先,我尝试获取父节点的 key (不确定这是不是正确的方法,但我最终得到了一个 datastore.Key 作为 parentKey).

当现在使用 parentKey 作为父项创建一个新 key ,然后尝试使用此 newKey put 项目时,我得到了 无效 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: %v\n", 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: %v\n", err)
return
}

我用 NameKeyIncompleteKey 都试过了,但都没有成功。

我显然在这里遗漏了一些东西,但无法弄清楚它是什么以及如何将我的项目作为另一个节点的子节点写入数据存储。

最佳答案

Tommy,您在评论中说到了。您需要在新 key 上设置命名空间。我在 Cloud Datastore Go documentation 中看不到任何方式要隐式执行此操作,因此您必须在调用 Put() 之前执行 newKey.namespace = parentKey.namespace。

关于Google Datastore 无效 key 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51964305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com