创建子记录时出现无效密钥消息。

huangapple go评论94阅读模式
英文:

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, &quot;programs&quot;, actKey)
_, err = datastore.Put(ctx, key, &amp;Program{Name: names[i]})

This passes:

key := datastore.NewIncompleteKey(ctx, &quot;programs&quot;, nil)
_, err = datastore.Put(ctx, key, &amp;Program{Name: names[i]})

Complete code:

	// insert test account
	actKey := datastore.NewIncompleteKey(ctx, &quot;accounts&quot;, nil)
	_, err = datastore.Put(ctx, actKey, &amp;Account{Name: &quot;Chris Olsenio&quot;})
	if err != nil {
		log.Errorf(ctx, &quot;Insert test account %v&quot;, err.Error())
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	var names = []string {&quot;Low Impact&quot;, &quot;Running&quot;}
	for i := 0; i &lt; len(names); i++ {
		key := datastore.NewIncompleteKey(ctx, &quot;programs&quot;, actKey)
		_, err = datastore.Put(ctx, key, &amp;Program{Name: names[i]})
		if err != nil {
			log.Errorf(ctx, &quot;Insert test programs %v&quot;, 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"})

如果errnilactKey现在将是一个完整的键,因此可以在使用datastore.NewIncompleteKey()datastore.NewKey()创建其他键时将其用作父键。

英文:

The problem is that when you create an incomplete key:

actKey := datastore.NewIncompleteKey(ctx, &quot;accounts&quot;, nil)

Which you use to save an entity:

_, err = datastore.Put(ctx, actKey, &amp;Account{Name: &quot;Chris Olsenio&quot;})

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 &quot;programs&quot; entities:

key := datastore.NewIncompleteKey(ctx, &quot;programs&quot;, 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 &quot;invalid key&quot; error message.

Solution is simple: store the generated new key, and pass the new, complete key as the parent key:

actKey := datastore.NewIncompleteKey(ctx, &quot;accounts&quot;, nil)
actKey, err = datastore.Put(ctx, actKey, &amp;Account{Name: &quot;Chris Olsenio&quot;})

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().

huangapple
  • 本文由 发表于 2016年4月21日 15:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/36762197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定