Golang GAE – 在Datastore中更改结构中的变量名

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

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接口,然后:

  1. Load方法中将BB复制到B
  2. Save方法中只保存AB
英文:

You can have your AA implement PropertyLoadSaver as described in the Datastore docs, an then

  1. in Load method copy BB into B
  2. in Save method just save A and B

答案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.

huangapple
  • 本文由 发表于 2012年10月21日 05:49:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/12993015.html
匿名

发表评论

匿名网友

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

确定