英文:
Getting Invalid Key message thrown when creating child records
问题
在使用父键为账户保存“程序”记录时,我遇到了问题。
这段代码会出现“无效键”的错误(完整错误信息请见底部):
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
这段代码是正确的:
key := datastore.NewIncompleteKey(ctx, "programs", nil)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
完整代码:
// 插入测试账户
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
_, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
if err != nil {
log.Errorf(ctx, "插入测试账户 %v", err.Error())
c.AbortWithError(http.StatusInternalServerError, err)
return
}
var names = []string {"Low Impact", "Running"}
for i := 0; i < len(names); i++ {
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
if err != nil {
log.Errorf(ctx, "插入测试程序 %v", err.Error())
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
英文:
I am having problems saving programs
records when using a parent key for the account.
This code fails with error "invalid key" (see bottom for complete):
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
This passes:
key := datastore.NewIncompleteKey(ctx, "programs", nil)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
Complete code:
// insert test account
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
_, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
if err != nil {
log.Errorf(ctx, "Insert test account %v", err.Error())
c.AbortWithError(http.StatusInternalServerError, err)
return
}
var names = []string {"Low Impact", "Running"}
for i := 0; i < len(names); i++ {
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
_, err = datastore.Put(ctx, key, &Program{Name: names[i]})
if err != nil {
log.Errorf(ctx, "Insert test programs %v", err.Error())
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
答案1
得分: 2
问题在于当你创建一个不完整的键时:
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
你用它来保存一个实体:
_, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
它可以工作,但请注意,如果传递的键是不完整的键(在你的情况下是这样),datastore.Put()
会返回一个由数据存储生成的新的唯一键。你没有存储返回的新键,但你应该存储它!
当你尝试创建和保存"programs"
实体时:
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
datastore.NewIncompleteKey()
期望一个nil
父键,如果提供了父键,它必须是一个完整的键(不能是不完整的)。你传递了一个不完整的键actKey
,因此出现了"invalid key"
错误消息。
解决方案很简单:存储生成的新键,并将新的完整键作为父键传递:
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
actKey, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
如果err
是nil
,actKey
现在将是一个完整的键,因此可以在使用datastore.NewIncompleteKey()
或datastore.NewKey()
创建其他键时将其用作父键。
英文:
The problem is that when you create an incomplete key:
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
Which you use to save an entity:
_, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
It works, but note that if the key passed is an incomplete key (it is in your case), datastore.Put()
returns a new, unique key generated by the datastore. You don't store the returned new key, but you should!
When you try to create and save "programs"
entities:
key := datastore.NewIncompleteKey(ctx, "programs", actKey)
datastore.NewIncompleteKey()
expects either a nil
parent key, of if it is provided, it must be a complete key (cannot be incomplete). You pass actKey
which is an incomplete key, hence the "invalid key"
error message.
Solution is simple: store the generated new key, and pass the new, complete key as the parent key:
actKey := datastore.NewIncompleteKey(ctx, "accounts", nil)
actKey, err = datastore.Put(ctx, actKey, &Account{Name: "Chris Olsenio"})
If err
is nil
, actKey
will now be a complete key and therefore can be used as parent key when creating other keys with datastore.NewIncompleteKey()
or datastore.NewKey()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论