如何使用“Put”将密钥输入RunInTransaction,而不必等到事务发生之后

问题描述 投票:0回答:1

数据库的结构我像链一样构建它,它看起来像这样:Click here to see the structure enter image description here

那些部分是:Click here to see what represents each part enter image description here

所以,当我想在我的链中添加新数据时:Click here to see the new data coming enter image description here,在我想要的任何地方,我可以通过更新结构的datastore.key轻松更新它们的值:click here to see the update enter image description here

所以,在这种情况下,我只需要更新b.NextBlock,c.LastBlock,e.LastBlock和e.NextBlock,一切都很好,但我想我想添加更多的新数据Click her to see new data coming enter image description here,我不想保存链条,如果任何数据失败¿我该怎么办?

因此,在两种情况下都要考虑通常使用“client.RunInTransaction”方法为每个新数据进行操作,所以我保证一切都很好,但这不可能,因为我无法获得“datastore.key”将数据附加到“client.RunInTransaction”时,文档说https://godoc.org/cloud.google.com/go/datastore#Transaction.Put(它返回* PendingKey没有键本身),我需要在“client.RunInTransaction”之外,以获取元素的“datastore.key”和“提交“正如文件说https://godoc.org/cloud.google.com/go/datastore#Commit.Key

所以,我希望函数“put”进入“client.RunInTransaction”,当代码在“client.RunInTransaction”中时,给我关键元素,之后没有,所以我可以保证更新一切正常,因为如果我有密钥后,下一个附加可能会失败,我不希望我的数据保存它

google-app-engine go google-cloud-datastore datastore app-engine-flexible
1个回答
1
投票

首先分别创建新数据实体,以获取其密钥。此时LastBlockNextBlock属性仍然是空的。

只有拥有实体的密钥后,才能使用事务在列表中执行实体的插入,在该列表中,您只更新该实体的关键引用以及实体所在的前一个和下一个实体(如果有)。插入。

© www.soinside.com 2019 - 2024. All rights reserved.