Proper way to migrate NDB model property

情到浓时终转凉″ 提交于 2019-12-10 21:51:24

问题


I currently have a model in NDB and I'd like to change the property name without necessarily touching NBD. Let's say I have the following:

from google.appengine.ext import ndb

class User(ndb.Model):
  company = ndb.KeyProperty(repeated=True)

What I would like to have is something more like this:

class User(ndb.Model):
  company_   = ndb.KeyProperty(repeated=True)

@property
def company(self):
   return '42'

@company.setter
def company(self, new_company):
    #set company here

Is there a relatively pain-free way to do so? I'd like the convienance of using property getter/setters, but given the current implementation I would like to avoid touching the underlying datastore.


回答1:


From my understanding of ndb, the property names are stored in the database along with their contents for every entity. You would have to rewrite every entity with the new property name (and without the old one).

Since that is not pain-free, maybe you could choose other names for your getter and setter like get_company and set_company.




回答2:


you can change the class-level property name while keeping the underlying NDB property name by specifying the name="xx" param in the Property() constructor

so something like this could be done:

class User(ndb.Model):
  company_   = ndb.KeyProperty(name="company", repeated=True)

  @property
  def company(self):
    return self.company_

  @company.setter
  def company(self, new_company):
    self.company_ = new_company

so now anytime you access .company_ NDB will actually set/get "company" internally... and you don't have to do any data migrations



来源:https://stackoverflow.com/questions/29527165/proper-way-to-migrate-ndb-model-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!