﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23916	makemigrations does not detect/like model name case changes	Sven Coenye	Adam Johnson	"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:

{{{
$ ./manage.py makemigrations
Migrations for 'as_migrations':
  0001_initial.py:
    - 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:

{{{
$ ./manage.py makemigrations
Migrations for 'as_migrations':
  0002_auto_20141125_1930.py:
    - 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:

{{{
$ ./manage.py makemigrations
Migrations for 'as_migrations':
  0003_auto_20141125_1930.py:
    - 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

{{{
$ ./manage.py makemigrations
Did you rename the as_migrations.Rubrictype model to RubricXype? [y/N] y
Migrations for 'as_migrations':
  0002_auto_20141125_1956.py:
    - 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 

{{{
migrations.RenameModel(
    old_name='Rubrictype',
    new_name='RubricType',
)
}}}

to the auto generated operations results in a ValueError exception when makemigrations is run again:

{{{
$ ./manage.py makemigrations
Traceback (most recent call last):
  File ""manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/__init__.py"", line 385, in execute_from_command_line
    utility.execute()
  File ""/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/__init__.py"", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/base.py"", 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/base.py"", line 338, in execute
    output = self.handle(*args, **options)
  File ""/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py"", line 111, in handle
    convert_apps=app_labels or None,
  File ""/home/svencoenye/developer/django_test/lib/python2.7/site-packages/django/db/migrations/autodetector.py"", 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/autodetector.py"", 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/state.py"", line 89, in render
    model=lookup_model,
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.
"	Bug	closed	Migrations	dev	Normal	fixed		info+coding@…	Ready for checkin	1	0	0	0	0	0
