Proper way to migrate NDB model property
Asked Answered
C

2

4

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.

Colecolectomy answered 8/4, 2015 at 23:34 Comment(3)
I'm not sure I follow why you want to change the property's name in NDB, but still use the old one in code. Are you planning to use the old name for something else? Or is it just a bad name? Maybe understanding this helps us help you :)Exhilarant
From my understanding, the model is saved in ndb with the field "company". In my app, I occasionally get and set company in the model, but I wanted to add some checking before setting it, and I thought it would be convenient to do some of that with a property setter. However, that would either require other bits of the code to access it with a different name, or I could change the field name within the model and let others access it the same way. I was seeing if I could preserve the fieldname and maintain it in the model.Colecolectomy
If you are doing "checking", you might want to use a validator, which allows you to validate and/or coerce the values before setting them... Search for validator here: cloud.google.com/appengine/docs/python/ndb/propertiesExhilarant
G
2

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.

Gladysglagolitic answered 8/4, 2015 at 23:45 Comment(0)
N
8

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

Nebraska answered 10/4, 2015 at 18:55 Comment(0)
G
2

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.

Gladysglagolitic answered 8/4, 2015 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.