To remove deleted properties from the datastore, GAE docs suggest this:
https://developers.google.com/appengine/articles/update_schema
It boils down to re-defining your class hierarchy as Expando, fetch, call
delattr, put, then changing model back.
What I've noticed is that even when you remove a property from your model,
you can still see the type and value in `_properties` and `_values`
properties on the object.
tl;dr - If I just remove it from those internal dicts and save, it seems
to work - that's a lot easier than what the docs above suggest -- is it
safe to do?
--
Full example showing what I mean:
Try this at shell.appspot.com: create an entity with a couple properties.
>>> class Foo(ndb.Model):
s = ndb.StringProperty()
s2 = ndb.StringProperty()
>>> f = Foo(id="foo", s="this is s1", s2="this is s2")
>>> f.put()
Then re-define the class without one of the props, and fetch the object
back:
>>> class Foo(ndb.Model):
s = ndb.StringProperty()
>>> g = Foo.get_by_id("foo")
Trying to access the deleted property directly will give you an
AttributeError, as you'd expect:
>>> g.s2
Traceback (most recent call last):
File "/base/data/home/apps/shell/1.335852500710379686/shell.py", line
267, in get
exec compiled in statement_module.__dict__
File "<string>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 's2'
But you can still "see" the remove property in the _properties and _values
arrays:
>>> g._properties
{'s2': GenericProperty('s2', repeated=0), 's': StringProperty('s')}
>>> g._values
{'s2': _BaseValue('this is s2'), 's': _BaseValue('this is s1')}
And -- this is the important part -- if you remove it from those internal
dicts and put(),
>>> del g._properties["s2"]
>>> del g._values["s2"]
>>> g.put()
it appears to remove the property from the datastore, and it's completely
gone when you re-fetch:
>>> h = Foo.get_by_id("foo")
>>> h._properties
{'s2': GenericProperty('s2', repeated=0), 's': StringProperty('s')}
This is a *a lot *easier than redefining models, uploading new versions,
then changing them back, re-uploading, etc.
Much rather just reach into the object, del the stale property and save.
Does anyone know if this is safe? Am I asking for trouble if I start to
clean up stale properties using this method?
Thanks for any answers, comments.
-ck
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/d/optout.