﻿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
22931	Migrations cannot rename intermediate models	JockeTF	nobody	"Due to a change in naming conventions I needed to rename an intermediate model. I renamed a model from Subscriber to Subscribe.

{{{#!python
from django.db import models
from django.conf import settings


class Channel(models.Model):
	name = models.CharField(max_length=128)
	subscribers = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Subscribe', related_name='subscriptions')
	date_created = models.DateTimeField(auto_now_add=True)


class Subscribe(models.Model):
	user = models.ForeignKey(settings.AUTH_USER_MODEL)
	channel = models.ForeignKey(Channel)
	date_subscribed = models.DateTimeField(auto_now_add=True)

	class Meta():
		unique_together = ('user', 'channel')
}}}

Running makemigrations in Django 1.7c1 results in an exception.

{{{#!python
  0002_auto_20140701_0817.py:
    - Create model Subscribe
    - Alter unique_together for subscribe (1 constraints)
Traceback (most recent call last):
  File ""./manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py"", line 385, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py"", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py"", line 115, in handle
    self.write_migration_files(changes)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/commands/makemigrations.py"", line 131, in write_migration_files
    self.stdout.write(""    - %s\n"" % operation.describe())
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/models.py"", line 251, in describe
    return ""Alter %s for %s (%s constraints)"" % (self.option_name, self.name, len(self.unique_together))
TypeError: object of type 'NoneType' has no len()
}}}

In Django 1.7b3 makemigrations actually worked.

{{{#!python
from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):
	dependencies = [
		('api', '0002_auto_20140630_1443'),
	]

	operations = [
		migrations.RenameModel(
			old_name='Subscriber',
			new_name='Subscribe',
		),
		migrations.AlterField(
			model_name='channel',
			name='subscribers',
			field=models.ManyToManyField(through='api.Subscribe', to=settings.AUTH_USER_MODEL),
		),
	]
}}}

However, running migrate in Django 1.7b3 results in another exception.

{{{#!python
Running migrations:
  Applying api.0013_subscriber_renamed...Traceback (most recent call last):
  File ""/usr/local/lib/python3.4/dist-packages/django/apps/config.py"", line 152, in get_model
    return self.models[model_name.lower()]
KeyError: 'subscriber'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/state.py"", line 78, in render
    model = self.apps.get_model(lookup_model[0], lookup_model[1])
  File ""/usr/local/lib/python3.4/dist-packages/django/apps/registry.py"", line 190, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File ""/usr/local/lib/python3.4/dist-packages/django/apps/config.py"", line 155, in get_model
    ""App '%s' doesn't have a '%s' model."" % (self.label, model_name))
LookupError: App 'api' doesn't have a 'subscriber' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""./manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py"", line 385, in execute_from_command_line
    utility.execute()
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py"", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
  File ""/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py"", line 146, in handle
    executor.migrate(targets, plan, fake=options.get(""fake"", False))
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py"", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py"", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py"", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/models.py"", line 141, in database_forwards
    new_apps = to_state.render()
  File ""/usr/local/lib/python3.4/dist-packages/django/db/migrations/state.py"", line 88, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field api.Channel.subscribers: api.Subscriber
}}}

The same exception is thrown if I move the migration generated in Django 1.7b3 to Django 1.7c1."	Bug	closed	Migrations	1.7	Normal	duplicate	migrations migrate makemigrations rename intermediate model	eschler@… info+coding@… vytis.banaitis@…	Accepted	0	0	0	0	0	0
