Opened 6 years ago

Closed 6 years ago

#29124 closed Bug (duplicate)

Generated migration orders RemoveField and AlterUniqueTogether incorrectly, causing FieldDoesNotExist

Reported by: Ed Morley Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Removing a field and its corresponding unique_together entry results in a FieldDoesNotExist exception when creating/applying the migration.

This affects Django master + v1.11.10, and both the SQLite and MySQL backends (others not tested).

(See also #29123 which also only reproduces when unique_together is used)


  1. Git clone
  2. pip install
  3. ./ migrate
  4. cp testapp/ testapp/
  5. ./ makemigrations --name broken_migration
  6. ./ migrate


New migration is created/applied successfully, which converts from the original model to the new model.


The new migration incorrectly lists the RemoveField operation before the AlterUniqueTogether operation...

    operations = [
            unique_together={('repository', 'revision')},

Which results in an exception at step 6...

$ ./ migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, testapp
Running migrations:
  Applying testapp.0002_broken_migration...Traceback (most recent call last):
  File ".../site-packages/django/db/models/", line 564, in get_field
    return self.fields_map[field_name]
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../site-packages/django/db/backends/base/", line 286, in create_model
    columns = [model._meta.get_field(field).column for field in fields]
  File ".../site-packages/django/db/backends/base/", line 286, in <listcomp>
    columns = [model._meta.get_field(field).column for field in fields]
  File ".../site-packages/django/db/models/", line 566, in get_field
    raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Push has no field named 'foo'

Additional notes:

  • If the ('repository', 'revision') entry in unique_together is omitted from both the original and new models, then the bug does not occur.
  • This affects both the SQLite backend and the MySQL backend (others not tested).
  • At time of testing, django master was at revision 6d794fb76212bb8a62fe2cd97cff173054e1c626.
  • The above was using Python 3.6.2.

Change History (1)

comment:1 by Tim Graham, 6 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #28862 I think.

Note: See TracTickets for help on using tickets.
Back to Top