views:

38

answers:

2

I have a google app engine app where I would like to extend one of my Entity definitions. How would I ensure existent entity objects get the new fields properly initialized? Would the existent objects, the next time I query them, simply have default values? I'd like to add a StringListProperty.

+3  A: 

You will have to add the property to all of your existing entities, one by one.

You don't mention which language or API you are using. The exact details of the procedure will vary with your situation.

In general, the safest way to do this is to load up each entity with a API that doesn't validate your entities. In python, you can use Expando models. In java, you can use the low level datastore API. (trying this with JDO or JPA may not work) You now need to iterate through all existing entities. (try the new Mapper API to do this with relatively little fuss). For each entity, you will load it, add your new property, then put/save it back to the datastore. Now you can safely go back to a framework that validates your entities, like JDO or non-expando models.

This method applies to modifying the type of a property or deleting a property as well.

Peter Recore
I am ok with supplying a default value (as Nick Johnson's answer suggests). Does your solution solve a problem that is more complex than that case, such as the new property's value being dependent upon something specific to each entity instance?
Joey
@Joey nick's solution is easier for your situation. changing the type of a property or deleting a property would require the more complicated method.
Peter Recore
+2  A: 

If you add a new property to your model, existing entities will have the default value for it when you load them, if you supplied a default. They won't show up in queries for that value until you fetch them and store them again, though.

Nick Johnson