#25417: decimal.InvalidOperation caused by Migrations and DecimalField()
-------------------------+-------------------------------------------------
     Reporter:  avorio   |      Owner:  nobody
         Type:  Bug      |     Status:  new
    Component:           |    Version:  1.8
  Migrations             |
     Severity:  Release  |   Keywords:  decimal, InvalidOperation,
  blocker                |  migrations
 Triage Stage:           |  Has patch:  0
  Unreviewed             |
Easy pickings:  0        |      UI/UX:  0
-------------------------+-------------------------------------------------
 Hello,

 I'm using Django version 1.8.4 and Python 3.4.3.

 I created this field:

 {{{
 tax_rate = models.DecimalField('tax rate (%)', max_digits=3,
 decimal_places=2, help_text='VAT rate', blank=True, null=True,
 default=20.00)
 }}}

 I then ran `python manage.py migrate` and got the database table created.
 All good.

 I then decided I wanted more digits, so I changed that field to:

 {{{
 tax_rate = models.DecimalField('tax rate (%)', max_digits=5,
 decimal_places=2, help_text='VAT rate', blank=True, null=True,
 default=20.00)
 }}}

 Now, when I run `python manage.py migrate` I get the following error:

 {{{
 File "manage.py", line 10, in <module>
   execute_from_command_line(sys.argv)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 338, in
 execute_from_command_line
   utility.execute()
 File "django-project/.venv/lib/python3.4/site-
 packages/django/core/management/__init__.py", line 330, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/core/management/base.py", line 393, in run_from_argv
   self.execute(*args, **cmd_options)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/core/management/base.py", line 444, in execute
   output = self.handle(*args, **options)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/core/management/commands/migrate.py", line 222, in handle
   executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/migrations/executor.py", line 110, in migrate
   self.apply_migration(states[migration], migration, fake=fake,
 fake_initial=fake_initial)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/migrations/executor.py", line 148, in apply_migration
   state = migration.apply(state, schema_editor)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/migrations/migration.py", line 115, in apply
   operation.database_forwards(self.app_label, schema_editor, old_state,
 project_state)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/migrations/operations/fields.py", line 201, in
 database_forwards
   schema_editor.alter_field(from_model, from_field, to_field)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/backends/base/schema.py", line 484, in alter_field
   old_db_params, new_db_params, strict)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/backends/base/schema.py", line 566, in _alter_field
   old_default = self.effective_default(old_field)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/backends/base/schema.py", line 211, in
 effective_default
   default = field.get_db_prep_save(default, self.connection)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/models/fields/__init__.py", line 1627, in
 get_db_prep_save
   self.max_digits, self.decimal_places)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/backends/base/operations.py", line 477, in
 value_to_db_decimal
   return utils.format_number(value, max_digits, decimal_places)
 File "django-project/.venv/lib/python3.4/site-
 packages/django/db/backends/utils.py", line 200, in format_number
   value = value.quantize(decimal.Decimal(".1") ** decimal_places,
 context=context)
 decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
 }}}

 Is this an actual bug, or am I doing something I shouldn't be?

--
Ticket URL: <https://code.djangoproject.com/ticket/25417>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.24a8839a80f779813099670c5a993500%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to