How to introduce required property in GAE

2019-04-29 20:31发布

I've changed my object to have a new required property in v2. When I attempt to fetch a v1 object from the datastore, I get BadValueError because v1 doesn't have the required property. What's the best way to introduce new required properties on existing data

2条回答
做自己的国王
2楼-- · 2019-04-29 20:52

I would resolve this problem using the mapreduce library.

First, register the mapper in mapreduce.yaml:

mapreduce:
- name: fixing required property
  mapper:
    input_reader: mapreduce.input_readers.DatastoreInputReader
    handler: your handler
    params:
    - name: entity_kind
      default: main.ModelV2

then define a process function to modify the entities:

from mapreduce import operation as op
def process(entity):
 if not entity.newproperty :
    entity.newproperty = None
 yield op.db.Put(entity)

If you are dealing with a relative small number of entities, you could avoid mapreduce modifying directly your entities with something like this:

entities = ModelV2.all()
for entity in entities :
  if not entity.newproperty :
    entity.newproperty = None
    entity.put()
查看更多
Root(大扎)
3楼-- · 2019-04-29 21:06

You'll need to add it as an optional property to your model, fetch each existing entity, add the property to it (generating a reasonable value somehow), then put() the entity. Once all of your existing entities have been "upgraded", you can make the property required.

The AppEngine mapreduce API should make this fairly easy.

查看更多
登录 后发表回答