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.