Opened 3 years ago

Last modified 8 months ago

#23916 new Bug

makemigrations does not detect/like model name case changes

Reported by: Sven Coenye Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords:
Cc: info+coding@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Starting with

class Evidence(models.Model):
    rubrictype = models.ForeignKey('Rubrictype')

class Rubrictype(models.Model):
    type_code = models.CharField(max_length=1)

Make the initial migration:

$ ./ makemigrations
Migrations for 'as_migrations':
    - Create model Evidence
    - Create model Rubrictype
    - Add field rubrictype to evidence

Change the name of Rubrictype to RubricType:

class Evidence(models.Model):
    rubrictype = models.ForeignKey('RubricType')

class RubricType(models.Model):
    type_code = models.CharField(max_length=1)

Generate the migration:

$ ./ makemigrations
Migrations for 'as_migrations':
    - Alter field rubrictype on evidence

Django does not detect the name change on the RubricType model itself. No confirmation is requested for the name change and no operation is generated. The problem is that any subsequent makemigrations run will generate the same operation ad infinitum:

$ ./ makemigrations
Migrations for 'as_migrations':
    - Alter field rubrictype on evidence

If instead the name is changed to RubricXype:

class Evidence(models.Model):
    rubrictype = models.ForeignKey('RubricXype')

class RubricXype(models.Model):
    type_code = models.CharField(max_length=1)

the corresponding migration becomes

$ ./ makemigrations
Did you rename the as_migrations.Rubrictype model to RubricXype? [y/N] y
Migrations for 'as_migrations':
    - Rename model Rubrictype to RubricXype

This migration generates a RenameModel operation only and any subsequent makemigrations runs will properly report "No changes detected". So it appears the change detector does not pick up on capitalization changes in model names.

Trying to work around by adding a


to the auto generated operations results in a ValueError exception when makemigrations is run again:

$ ./ makemigrations
Traceback (most recent call last):
  File "", line 10, in <module>
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/", line 385, in execute_from_command_line
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/", line 377, in execute
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/commands/", line 111, in handle
    convert_apps=app_labels or None,
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/db/migrations/", line 42, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/db/migrations/", line 109, in _detect_changes
    self.old_apps = self.from_state.render(ignore_swappable=True)
  File "/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/db/migrations/", line 89, in render
ValueError: Lookup failed for model referenced by field as_migrations.Evidence.rubrictype: as_migrations.RubricType

The sequence of the operations does not matter. Neither does substituting the RenameModel operation for the AlterField operation.

(Looking at the next to last entry in the traceback, the autodetector seems to be looking for the new name in the old_apps state?)

It is possible, however, to go the long way around and use two separate migrations: Rubrictype -> RubricXype. RubricXype -> RubricType works without getting the migration state stuck and does not throw an exception.

Change History (6)

comment:1 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 3 years ago by Markus Holtermann

Cc: info+coding@… added

Thank you for the report. The problem you ran into relates to the fact that the migration internally don't care about case sensitivity of model names (ProjectState.models has a dictionary whose keys are (app_label, model_name) where the latter is lower cased).

Your work around seems to be valid. I'd need more info to figure out why adding the RenameModel manually fails.

comment:3 Changed 3 years ago by Sven Coenye

Sorry for the delayed response. I did not realize my e-mail address was missing from the preferences.

Is there anything I can do to provide more info on the RenameModel failure?

comment:4 Changed 2 years ago by Tim Graham

See #25429 for a probable duplicate (but please check when this is fixed and reopen it if not).

comment:5 Changed 16 months ago by Tim Graham

#26752 seems to be another symptom (closed as duplicate, but reopen if not).

comment:6 Changed 8 months ago by Anton Samarchyan

This is a duplicate of #27297

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