2
votes

I'm working in Google App Engine, Python 2.7, Standard Environment, using the ndb datastore. I'm doing some updates to some of my NDB models, and one of the updates requires me to add a new property (a floating-point number) to my "Restaurant" model. In the past, when I've needed to add properties to a model, I've always liked how NDB lets you specify default values when defining the model, like so:

class Restaurant(ndb.Model):
    ... some properties ...
    resto_fullresyncstamp = ndb.FloatProperty(default=0.0)

My understanding of this default value was, when the entity was fetched from the datastore, if there is no resto_fullresyncstamp property for a given entity, one is added to the local copy of the model, with the default value. That way, I don't have to keep checking to see if everything is None, and I can assume the property will always have a value in my code. So I should be able to do something like this ...

lastsync = ...
resto = Restaurant.query()....get()
if not resto:
    logging.warning("Failed to get restauraunt: %s" % username)
    self.error(404)
    return

if lastsync != 0.0 and resto.resto_fullresyncstamp > lastsync:
    logging.warning("Requiring client to perform full resynchronization")
    self.error(510)
    return

... and expect that, even if "resto_fullresyncstamp" is a new property in my model, and the particular restaurant has never had that property set explicitly anywhere, that I will still get a valid (0.0) value to compare against.

What I'm actually getting is this error message:

Traceback (most recent call last):
....
File "/base/data/home/apps/s~virt-som/1.406133528491612487/virtsom.py", line 5399, in post
if lastsync != 0.0 and resto.resto_fullresyncstamp > lastsync:

AttributeError: 'Restaurant' object has no attribute 'resto_fullresyncstamp'

Shouldn't this work? I've been working with NDB for ages, and I've used the "default" parameter in my Models like this successfully before. Am I missing something?

1

1 Answers

0
votes

That's not how it works.

Whenever you add a new property to a existing model you have to put the old entities again into the datastore to get that property. To get it at all no matter what the default value is.