英文:
Golang GAE - Change a variable name in a structure in Datastore
问题
我正在编写一个Google Apps Engine Go应用程序,我想在数据存储中更改一个结构体中的变量名。
假设我有一个结构体:
type AA struct{
A string
BB string
}
我想将BB
更改为B
。如果我尝试直接将BB
更改为B
,当数据存储尝试将存储的BB
值分配给不具有该变量的新结构体AA
时,数据存储将开始给我报错。我可以添加B
并保留BB
,但是结构体会变得混乱。
如何在GAE Go数据存储中整洁地更改变量结构,而不需要临时复制整个数据库并清除大量数据?
英文:
I am programming a Google Apps Engine Go application and I would like to change a variable name inside a structure that is stored in the datastore.
Say I have a struct:
type AA struct{
A string
BB string
}
And would like to change BB
into B
. If I try just changing BB
into B
, the datastore will start giving me errors when it would try to assign stored BB
values to new struct AA
that does not have that variable. I can add B
and still keep BB
, but then the struct would start getting messy.
How can I neatly change variable structure in GAE Go datastore without resorting to temporary copying over the entire database and wiping a lot of data?
答案1
得分: 3
你可以按照Datastore文档中描述的方式,让你的AA
实现PropertyLoadSaver
接口,然后:
- 在
Load
方法中将BB
复制到B
- 在
Save
方法中只保存A
和B
英文:
You can have your AA
implement PropertyLoadSaver
as described in the Datastore docs, an then
- in
Load
method copyBB
intoB
- in
Save
method just saveA
andB
答案2
得分: 0
请查看App Engine文档中关于更新模型架构的部分,该部分描述了您需要按照的流程来更新架构,然后删除过时的属性。
希望对您有所帮助。
英文:
Take a look at the App Engine documentation about Updating Your Model's Schema tha describes the flow you need to follow in order to update your schema and then delete obsolete properties.
Hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论