﻿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
22944	Migrate doesn't like ForeignKey being reassigned to a different app's model	mozumder@…	Andrew Godwin	"Hi,

Django 1.7 RC1 doesn't like it when I try to migrate an app's models.

I have a couple of models in App ""Background"". Simplified example:


{{{
class Family(models.Model):
    name = models.CharField(
                        _(""name""),
                        max_length=255,
                        blank=True,
                        null=True,
                        )
    order = models.PositiveSmallIntegerField(
                    _(""order""),
                    default=0,
                    )
    parent = models.ForeignKey(
                        'self',
                        blank=True,
                        null=True,


class ImageLayer(models.Model):

    family = models.ForeignKey(
        Family,
        verbose_name=_(""Family Name""),
        related_name='background_family_name',
        null=True,
        blank=True,
        )

}}}
        
I have another separate App, ""Styles"" with a similar Family model:


{{{
class StyleFamily(models.Model):
    name = models.CharField(
                        _(""name""),
                        max_length=255,
                        blank=True,
                        null=True,
                        )
    order = models.PositiveSmallIntegerField(
                    _(""order""),
                    default=0,
                    )
    parent = models.ForeignKey(
                        'self',
                        blank=True,
                        null=True,
                    )

}}}

Now, I try to delete my ""Family"" model from the Background App, and reassign the family field to 'styles.StyleFamily"" as:


{{{
class ImageLayer(models.Model):

    family = models.ForeignKey(
        'styles.StyleFamily',
        verbose_name=_(""Family Name""),
        related_name='background_family_name',
        null=True,
        blank=True,
        )

}}}

Then I run manage.py makemigration and manage.py migrate

I get the following:


{{{
bobby:engine mozumder$ ./manage.py makemigrations
Migrations for 'background':
  0002_auto_20140702_1621.py:
    - Remove field parent from family
    - Delete model Family
    - Alter field family on imagelayer
    - Alter field family on stack
bobby:engine mozumder$ ./manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: grappelli
  Apply all migrations: sessions, styles, background, blocks, borders, defaults, effects, structure, transform, sites, contenttypes, admin, typography, content, animation, auth, colors, redirects, layout, text, articles, categories
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying background.0002_auto_20140702_1621...Traceback (most recent call last):
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py"", line 152, in get_model
    return self.models[model_name.lower()]
KeyError: 'family'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py"", line 79, in render
    model = self.apps.get_model(lookup_model[0], lookup_model[1])
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py"", line 190, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py"", line 155, in get_model
    ""App '%s' doesn't have a '%s' model."" % (self.label, model_name))
LookupError: App 'background' doesn't have a 'family' 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 ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py"", line 385, in execute_from_command_line
    utility.execute()
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py"", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py"", line 160, in handle
    executor.migrate(targets, plan, fake=options.get(""fake"", False))
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py"", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py"", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py"", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py"", line 118, in database_forwards
    from_model = from_state.render().get_model(app_label, self.model_name)
  File ""/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py"", line 89, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field background.ImageLayer.family: background.Family

}}}

It seems it doesn't like it when I reassign the ""family"" field to a ForeignKey outside the App. This happens even with empty ""Family"" and ""styles.Family"" models.

I avoid this situation by rebuilding the database from scratch and calling in fixtures.   I think this is probably a bug?

This is with Django 1.7 RC1, Python 3.4.0,  and Postgres 9.3.4

Anyways, let me know if there is a workaround... thank you!

-bobby"	Bug	closed	Migrations	1.7-rc-2	Release blocker	fixed			Accepted	0	0	0	0	0	0
