0
votes

I currently have a model in NDB and I'd like to add a new property to it. Let's say I have the following:

class User(Model, BaseModel):
    name = ndb.StringProperty(required=False)
    email = ndb.StringProperty(required=False)

    @property
    def user_roles(self):
        return UserRole.query(ancestor=self.key).fetch()

    @property
    def roles(self):
        return [user_role.role for user_role in UserRole.query(ancestor=self.key).fetch()]

Now, let's say, I've added one additional property called market_id. For example,

class User(Model, BaseModel):
    name = ndb.StringProperty(required=False)
    email = ndb.StringProperty(required=False)

    @property
    def user_roles(self):
        return UserRole.query(ancestor=self.key).fetch()

    @property
    def roles(self):
        return [user_role.role for user_role in UserRole.query(ancestor=self.key).fetch()]

    @property
    def market_id(self):
        """ fetches `id` for the resource `market` associated with `user` """
        for each_role in UserRole.query(ancestor=self.key):
            resource = each_role.role.get().resource
            if resource.kind() == 'Market':
                return resource.id()

        return None

The problem here is, roles are fetched properly as expected for all the existing entities (since that property had been there since the beginning and also, an extra column can be observed in datastore called roles).

Since, I'm dealing with Python class property, I assume that migration is not required. But, how does column called roles already exist? And why newly added property called market_id does not? Does it require migration?

1

1 Answers

3
votes

The change you're suggesting is not an actual ndb model change as you're not adding/deleting/modifying any of the model's datastore properties. Only ndb.Property class children are real ndb model properties that are stored when the entity is put() into the datastore.

The property you're adding is a Python class @property - nothing to do with what's in the datastore.

So for this particular case no migration is needed.

The update to the question makes this even more clear, I believe. The market_id @property is not a User datastore entity property. To get values for it you don't need to update the User entity, but you have to create/edit corresponding UserRole entities with their resource property point to a Market entity.