Django generated migration fails with KeyError for field that was removed from the model.

1,681 views
Skip to first unread message

jackotonye

unread,
Apr 19, 2018, 12:16:24 AM4/19/18
to Django users
A migration generated with `python manage.py makemigrations` Fails to execute using `python manage.py migrate`.


Model Layout:

class Offer(BaseModel):
    client
= models.ForeignKey(Client, on_delete=models.PROTECT, related_name='offers')
   
amount_off = models.DecimalField(max_digits=5, decimal_places=2, default=Decimal('0.00')).  # Removed this field from the model
   


Generated Migration file:

# Generated by Django 2.0.2 on 2018-04-18 21:36

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
       
('my_app', '0031_auto_20180418_1802'),
    ]

    operations
= [
        migrations
.RemoveField(
           
model_name='offer',
            name='amount_off',
        ),
   
]




Error:
Running migrations:

 
Applying my_app.0032_auto_20180418_2136...Traceback (most recent call last):

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/models/options.py", line 566, in get_field

   
return self.fields_map[field_name]

KeyError: 'amount_off'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

 
File "manage.py", line 22, in <module>

    execute_from_command_line
(sys.argv)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line

    utility
.execute()

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute

   
self.fetch_command(subcommand).run_from_argv(self.argv)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv

   
self.execute(*args, **cmd_options)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute

    output
= self.handle(*args, **options)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle

    fake_initial
=fake_initial,

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate

    state
= self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards

    state
= self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration

    state
= migration.apply(state, schema_editor)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply

    operation
.database_forwards(self.app_label, schema_editor, old_state, project_state)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 525, in database_forwards

    getattr
(new_model._meta, self.option_name, set()),

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 342, in alter_unique_together

   
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 365, in _delete_composed_index

    columns
= [model._meta.get_field(field).column for field in fields]

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 365, in <listcomp>

    columns
= [model._meta.get_field(field).column for field in fields]

 
File "/Users/myuser/.virtualenvs/my_app/lib/python3.6/site-packages/django/db/models/options.py", line 568, in get_field

   
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))

django
.core.exceptions.FieldDoesNotExist: Offer has no field named 'amount_off'







James Farris

unread,
Apr 19, 2018, 12:37:52 PM4/19/18
to django...@googlegroups.com
My guess is that field is not empty in the database or it’s being called in a view or template still. 

Sent from my mobile device
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/423f74f4-3966-4f0f-ba20-a9008d240966%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jani Tiainen

unread,
Apr 19, 2018, 1:14:55 PM4/19/18
to django...@googlegroups.com
Hi,

Is that complete model definition?

Since exception seems to go through modifying composed index (unique_together) where you might still have reference to deleted field.


To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...
Reply all
Reply to author
Forward
0 new messages