Reputation: 1653
I created and subsequently changed a DecimalField in my models. Now, when I try to run python manage.py migrate
I get the following error:
decimal.InvalidOperation: quantize result has too many digits for current context
After reading the various related questions here on SO I have tried increasing the max_digits, which doesn't help. I have manually deleted the model in my postgreSQL database expecting to be able to recreate it when I migrate the models but I still get this same error and am not able to migrate the models and recreate the model in the database.
This is the model in question:
class Version(models.Model):
version_number = models.DecimalField(default=1.0, max_digits=3,
decimal_places=2)
What can I do to remedy this?
EDIT When I changed the DecimalField I changed the max_digits
Upvotes: 1
Views: 1373
Reputation: 1098
So i had the same issue, no matter how i changed the existing DecimalField, i kept getting this error (decimal.InvalidOperation)
To look it up, i set a pdb/set_trace in django utils.py (...\site-packages\django\db\backends) to see what is causing this exception
On my example it was using max_digits=4 and decimal_places=4 (which again, wasn't the most recent values for this model's field). So i went up in the functions, and found out that one of the early migrations was causing this.
I changed the migration and all other migrations that related to this model's field to be the recent version and it was fixed
Upvotes: 1
Reputation: 6968
It seems your problem isn't actually with DecimalField, but with the manual deletion of your table as a troubleshooting step.
As far as I know, the makemigrations
tool doesn't make changes according to what is in the existing database, but rather what has been defined in prior migrations.
So if you change something without using it, you'll have to manually create or edit the migration file to correct it.
See the following for examples of editing migration files:
https://docs.djangoproject.com/en/1.8/howto/writing-migrations/
I'm not sure what the protocol is here, but it might be possible to locate the migration where the table was first created and copypaste the applicable sequence into your pending migration file.
Upvotes: 5
Reputation: 1285
Ok so you have problem with setting the default value here:
You need to import Decimal.
from decimal import Decimal
class Version(models.Model):
version_number = models.DecimalField(default=Decimal('1.00'), max_digits=3,
decimal_places=2)
Upvotes: 2