普通网友 2018-08-22 09:58
浏览 38
已采纳

Google数据存储区无效密钥错误

I'm trying to put an item to the Google Datastore using golang.

I'm always running into a datastore: invalid key error though and can't figure out what's wrong here. I'm using the "cloud.google.com/go/datastore" package.

First I try to get the key for the parent node (not sure this is the right way to do it, but I do end up getting a datastore.Key as parentKey).

When now creating a new key using the parentKey as parent and then trying to put the item with this newKey I get the invalid key error message.

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
", 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
", err)
    return
}

I tried it both with the NameKey and IncompleteKey but no luck with either.

I'm obviously missing something here but can't figure out what it is and how to write my item to the datastore as the child of the other node.

  • 写回答

1条回答 默认 最新

  • douwen5951 2018-08-25 00:03
    关注

    Tommy, you nailed it in your comment. You need to set the namespace on your new key. I don't see a way in the Cloud Datastore Go documentation to do this implicitly, so you'll have to do newKey.namespace = parentKey.namespace before calling Put().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义