TJ1S
TJ1S

Reputation: 528

Proper way to migrate NDB model property

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.

Upvotes: 4

Views: 590

Answers (2)

Nicholas Franceschina
Nicholas Franceschina

Reputation: 6147

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

Upvotes: 8

Brent Washburne
Brent Washburne

Reputation: 13138

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.

Upvotes: 2

Related Questions