Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change IntegerProperty to FloatProperty of existing AppEngine DataStore

I built an appengine application (python) which need to convert existing datastore entities in integer value (100) to float value (100.00) for currency conversion issue. How's the right way doing this? Since my query returning error when i just change property type in my model.

Old Model:

class Learn(search.SearchableModel):
    pid = db.ReferenceProperty(Product, collection_name='picks')
    title = db.StringProperty()
    description = db.TextProperty()
    order = db.IntegerProperty()
    cost = db.IntegerProperty(default=0)
    cost1 = db.IntegerProperty(default=0)

New Model:

class Learn(search.SearchableModel):
    pid = db.ReferenceProperty(Product, collection_name='picks')
    title = db.StringProperty()
    description = db.TextProperty()
    order = db.IntegerProperty()
    cost = db.FloatProperty(default=0.000)
    cost1 = db.FloatProperty(default=0.000)

I need a proper way to alter this datastore property type without changing (delete old and add new) existing data. Since it's key used in many others table/model.

Thanks.

like image 743
Ivan Slaughter Avatar asked Jan 20 '11 02:01

Ivan Slaughter


2 Answers

The easiest way to do this is to change the model to inherit from db.Expando, and delete the integer properties from the definion. Then, load each instance and do "instance.foo = float(instance.foo)" on each, before saving them back to the datastore - you'll probably want to use the mapreduce API for this. Finally, make the model extend db.Model again, and add the FloatProperties back.

You really, really don't want to use a float for currency, though: floats are susceptible to rounding errors, which means you can lose (or gain!) money. Instead, use an IntegerProperty that counts the number of cents.

like image 102
Nick Johnson Avatar answered Sep 28 '22 06:09

Nick Johnson


Here is exampe for Nick Johnson's answer:

Before:

class Person(db.Model):
    name = db.StringProperty()
    age = db.StringProperty() #this will go to int

After

class Person(db.Expando):
    pass

for person in Person.all():
    person.age = int(person.age)
    person.put()

Very after:

class Person(db.Model):
    name = db.StringProperty()
    age = db.IntegerProperty() 
like image 26
asdf_enel_hak Avatar answered Sep 28 '22 07:09

asdf_enel_hak